private void WatchFolders() { // Release existing FSW Objects first foreach (DelayedFileSystemWatcher watcher in _Watchers) { watcher.EnableRaisingEvents = false; watcher.Dispose(); } _Watchers.Clear(); try { _Events = ArrayList.Synchronized(new ArrayList(64)); // Create the watchers. //I need 2 type of watchers. 1 for files and the other for directories // Reason is that i don't know if the event occured on a file or directory. // For a Create / Change / Rename i could figure that out using FileInfo or DirectoryInfo, // but when something gets deleted, i don't know if it is a File or directory DelayedFileSystemWatcher watcherFile = new DelayedFileSystemWatcher(); DelayedFileSystemWatcher watcherDirectory = new DelayedFileSystemWatcher(); watcherFile.Path = _currentFolder; watcherDirectory.Path = _currentFolder; /* Watch for changes in LastWrite times, and the renaming of files or directories. */ watcherFile.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Attributes; watcherDirectory.NotifyFilter = NotifyFilters.DirectoryName; watcherFile.Filter = "*.*"; watcherFile.IncludeSubdirectories = false; watcherDirectory.Filter = "*.*"; watcherDirectory.IncludeSubdirectories = false; // Add event handlers. watcherFile.Changed += new FileSystemEventHandler(OnChanged); watcherFile.Created += new FileSystemEventHandler(OnCreated); watcherFile.Deleted += new FileSystemEventHandler(OnDeleted); watcherFile.Renamed += new RenamedEventHandler(OnRenamed); watcherDirectory.Deleted += new FileSystemEventHandler(OnDirectoryDeleted); watcherDirectory.Renamed += new RenamedEventHandler(OnDirectoryRenamed); watcherDirectory.Created += new FileSystemEventHandler(OnDirectoryCreated); // Begin watching. watcherFile.EnableRaisingEvents = true; watcherDirectory.EnableRaisingEvents = true; _Watchers.Add(watcherFile); _Watchers.Add(watcherDirectory); // Start Timer for processing events _Timer = new Timer(_TimerInterval); _Timer.Elapsed += new ElapsedEventHandler(ProcessEvents); _Timer.AutoReset = true; _Timer.Enabled = watcherFile.EnableRaisingEvents; } catch (Exception ex) { Log.Error("PicturesFolderWatcher Unable to turn on monitoring for: {0} Exception: {1}", _currentFolder, ex.Message); } }
private void WatchFolders() { // Release existing FSW Objects first foreach (DelayedFileSystemWatcher watcher in _Watchers) { watcher.EnableRaisingEvents = false; watcher.Dispose(); } _Watchers.Clear(); try { _Events = ArrayList.Synchronized(new ArrayList(64)); // Create the watchers. //I need 2 type of watchers. 1 for files and the other for directories // Reason is that i don't know if the event occured on a file or directory. // For a Create / Change / Rename i could figure that out using FileInfo or DirectoryInfo, // but when something gets deleted, i don't know if it is a File or directory DelayedFileSystemWatcher watcherFile = new DelayedFileSystemWatcher(); DelayedFileSystemWatcher watcherDirectory = new DelayedFileSystemWatcher(); watcherFile.Path = _currentFolder; watcherDirectory.Path = _currentFolder; /* Watch for changes in LastWrite times, and the renaming of files or directories. */ watcherFile.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Attributes; watcherDirectory.NotifyFilter = NotifyFilters.DirectoryName; watcherFile.Filter = "*.*"; watcherFile.IncludeSubdirectories = false; watcherDirectory.Filter = "*.*"; watcherDirectory.IncludeSubdirectories = false; // Add event handlers. watcherFile.Changed += new FileSystemEventHandler(OnChanged); watcherFile.Created += new FileSystemEventHandler(OnCreated); watcherFile.Deleted += new FileSystemEventHandler(OnDeleted); watcherFile.Renamed += new RenamedEventHandler(OnRenamed); watcherDirectory.Deleted += new FileSystemEventHandler(OnDirectoryDeleted); watcherDirectory.Renamed += new RenamedEventHandler(OnDirectoryRenamed); watcherDirectory.Created += new FileSystemEventHandler(OnDirectoryCreated); // Begin watching. watcherFile.EnableRaisingEvents = true; watcherDirectory.EnableRaisingEvents = true; _Watchers.Add(watcherFile); _Watchers.Add(watcherDirectory); // Start Timer for processing events _Timer = new Timer(_TimerInterval); _Timer.Elapsed += new ElapsedEventHandler(ProcessEvents); _Timer.AutoReset = true; _Timer.Enabled = watcherFile.EnableRaisingEvents; } catch (ArgumentException ex) { Log.Error("PicturesFolderWatcher Unable to turn on monitoring for: {0} Exception: {1}", _currentFolder, ex.Message); } }