public void WatchFile(string filePath, FileChangedCallback callBack, ISynchronizeInvoke synchronizeInvoke = null) { var watcher = new FileSystemWatcher(); watcher.Path = Path.GetDirectoryName(filePath); /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcher.Filter = Path.GetFileName(filePath); // Add event handlers. watcher.Changed += new FileSystemEventHandler((object o, FileSystemEventArgs a) => { if (callBack != null) { callBack(filePath); } }); //watcher.Created += new FileSystemEventHandler((object o, FileSystemEventArgs a) => { }); //watcher.Deleted += new FileSystemEventHandler((object o, FileSystemEventArgs a) => { }); watcher.Renamed += new RenamedEventHandler((object o, RenamedEventArgs a) => { if (callBack != null) { callBack(filePath); } }); if (synchronizeInvoke != null) { watcher.SynchronizingObject = synchronizeInvoke; } watcher.EnableRaisingEvents = true; watchers.Add(watcher); }
/// <summary> /// The constructor for the watcher /// </summary> /// <param name="a_logger"> /// instance of logger /// </param> /// <param name="a_CallbackMethod"> /// call back method /// </param> public FileSystemWatcherHelper(ILogger a_logger, FileChangedCallback a_CallbackMethod) { if (a_logger != null) { _Logger = a_logger; } if (a_CallbackMethod != null) { _FileChangedCallbackMethod = a_CallbackMethod; } _lastFileEvent = new Dictionary <string, DateTime>(); Interval = 100; FilterRecentEvents = true; Intialize(); }
internal static void Register(string path, FileChangedCallback onFileChanged) { new FileChangeNotifier(path, onFileChanged); }
private FileChangeNotifier(string path, FileChangedCallback onFileChanged) { _onFileChanged = onFileChanged; RegisterForNextNotification(path); }