public static void FileWatchThread(object param) { string fullfilename = (string)param; FileSystemWatcher2 fsw = null; try { var dir = Path.GetDirectoryName(fullfilename); var backupdir = Path.Combine(dir, "FileWatcherBackups"); Directory.CreateDirectory(backupdir); fsw = new FileSystemWatcher2(dir); fsw.Filter = Path.GetFileName(fullfilename); fsw.NotifyFilter = NotifyFilters.LastWrite; fsw.Changed += FileSystemWatcher_Changed; fsw.EnableRaisingEvents = true; while (!stopFlag) { Thread.Sleep(1000); } } catch (Exception ex) { LogText("***FileWatchThread EXCEPTION***\n" + fullfilename + " >> " + (fsw != null).ToString() + "\n" + GetExceptionMessage(ex), Color.LightCoral); } finally { if (fsw != null) { fsw.EnableRaisingEvents = false; fsw.Dispose(); } } }
private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e) { FileSystemWatcher2 fsw = (FileSystemWatcher2)sender; fsw.EnableRaisingEvents = false; try { //LogText(e.Name + " >> " + fsw.ControlNumber.ToString("D4"), Color.White); string newfilename = String.Format("{0:D4}_{1}", fsw.ControlNumber, e.Name); string source = Path.Combine(fsw.Path, e.Name); string destination = Path.Combine(fsw.Path, "FileWatcherBackups", newfilename); File.Copy(source, destination); LogText(e.Name + " >> " + fsw.ControlNumber.ToString("D4") + " copy OK", Color.Cyan); fsw.ControlNumber++; } catch (Exception ex) { LogText("***FileSystemWatcher_Changed EXCEPTION***\n" + e.Name + " >> " + fsw.ControlNumber.ToString("D4") + "\n" + GetExceptionMessage(ex), Color.LightCoral); } finally { fsw.EnableRaisingEvents = true; } }
public AppContext() { properties = ConfigINI.GetInstance().Items; if (properties.ContainsKey("SOURCE_PATH")) { SourcePath = properties["SOURCE_PATH"].ToString(); if (!FileFunctions.FileFunctions.IsValidRootedPath(SourcePath)) { MessageBox.Show($"Source path \"{SourcePath}\" is not a valid rooted path.", "Fermeture du programme"); Application.Exit(); } } else { throw new Exception("Source path is undefined."); } if (properties.ContainsKey("DESTINATION_PATH")) { DestinationPath = properties["DESTINATION_PATH"].ToString(); if (!FileFunctions.FileFunctions.IsValidRootedPath(DestinationPath)) { MessageBox.Show($"Destination path \"{DestinationPath}\" is not a valid rooted path.", "Fermeture du programme"); Application.Exit(); } } else { throw new Exception("Destination path is undefined."); } CreateSystemTrayIcon(); Application.ApplicationExit += OnApplicationExit; FileSystemWatcher2.EventIDs events = FileSystemWatcher2.EventIDs.NONE; if (properties.ContainsKey("MANAGE_EVENTS")) { foreach (string eventName in properties["MANAGE_EVENTS"].ToString().Split(',')) { switch (eventName) { case "Created": events |= FileSystemWatcher2.EventIDs.CREATED; break; case "Changed": events |= FileSystemWatcher2.EventIDs.CHANGED; break; case "Renamed": events |= FileSystemWatcher2.EventIDs.RENAMED; break; case "Deleted": events |= FileSystemWatcher2.EventIDs.DELETED; break; default: // This is an invalid event name. break; } } } else { events = FileSystemWatcher2.EventIDs.ALL; } fileSystemWatcher = new FileSystemWatcher2(SourcePath, DestinationPath, events); StartFileSystemWatcher(); }