private void OnFileWatcherCreated(object sender, FileSystemEventArgs e) { mutex.WaitOne(); string path = e.FullPath; string name = System.IO.Path.GetFileName(path); int index = ExpectingFiles.IndexOf(path); if (index >= 0) { ExpectingFiles.RemoveAt(index); WatchedFiles.Add(path); ItemAdded?.Invoke(this, new WatchingItemEventArgs(ItemType.File, path, name)); } else { foreach (DirectoryScanOptions scanOptions in ScanOptions) { if (scanOptions.IsIncluded(name, path, ItemType.File)) { ItemAdded?.Invoke(this, new WatchingItemEventArgs(ItemType.File, path, name)); break; } } } mutex.Set(); }
private void OnFileWatcherRenamed(object sender, RenamedEventArgs e) { mutex.WaitOne(); string removedPath = e.OldFullPath; string removedName = System.IO.Path.GetFileName(removedPath); string addedPath = e.FullPath; string addedName = System.IO.Path.GetFileName(addedPath); int index = WatchedFiles.IndexOf(removedPath); if (index >= 0) { WatchedFiles.RemoveAt(index); ExpectingFiles.Add(removedPath); ItemRemoved?.Invoke(this, new WatchingItemEventArgs(ItemType.File, removedPath, removedName)); } else { foreach (DirectoryScanOptions scanOptions in ScanOptions) { if (scanOptions.IsIncluded(removedName, removedPath, ItemType.File)) { ItemRemoved?.Invoke(this, new WatchingItemEventArgs(ItemType.File, removedPath, removedName)); break; } } } index = ExpectingFiles.IndexOf(addedPath); if (index >= 0) { ExpectingFiles.RemoveAt(index); WatchedFiles.Add(addedPath); ItemAdded?.Invoke(this, new WatchingItemEventArgs(ItemType.File, addedPath, addedName)); } else { foreach (DirectoryScanOptions scanOptions in ScanOptions) { if (scanOptions.IsIncluded(addedName, addedPath, ItemType.File)) { ItemAdded?.Invoke(this, new WatchingItemEventArgs(ItemType.File, addedPath, addedName)); break; } } } mutex.Set(); }
private void WatchDocumentForChanges(string fullFilePath, Action <object, FileSystemEventArgs> onChangedAction) { if (!WatchedFiles.ContainsKey(fullFilePath)) { var fsw = new FileSystemWatcher(); fsw.BeginInit(); fsw.Path = Path.GetDirectoryName(fullFilePath); fsw.Filter = Path.GetFileName(fullFilePath); fsw.IncludeSubdirectories = false; fsw.NotifyFilter = NotifyFilters.LastWrite; fsw.Changed += onChangedAction.Invoke; fsw.EnableRaisingEvents = true; fsw.EndInit(); WatchedFiles.Add(fullFilePath, fsw); } }