FileSystemWatcher for tracking moved files

Possible duplicate:
Detecting Moved Files Using FileSystemWatcher

I am trying to track moved files using FileSystemWatcher and get some help along the way here:

Using FileSystemWatcher with Multiple Files

However, I found that I had to use both deleted and created events to get the paths from which the files were moved, as well as the path to where they were moved. But when I add the same code for the Delete event, I can only get one or the other event to fire. And that seems to be the order of where I spend events that determine which event will be executed. Therefore, if I put the "Created" event last in the posting code that will be triggered, and vice versa, if I put the "Delete" posting last, which will be triggered but not created.

Here is the code:

public class FileListEventArgs : EventArgs { public List<string> FileList { get; set; } } public class Monitor { private List<string> filePaths; private List<string> deletedFilePaths; private ReaderWriterLockSlim rwlock; private Timer processTimer; private Timer deletionTimer; public event EventHandler FileListCreated; public event EventHandler FileListDeleted; public void OnFileListCreated(FileListEventArgs e) { if (FileListCreated != null) FileListCreated(this, e); } public void OnFileListDeleted(FileListEventArgs e) { if (FileListDeleted != null) FileListDeleted(this, e); } public Monitor(string path) { filePaths = new List<string>(); deletedFilePaths = new List<string>(); rwlock = new ReaderWriterLockSlim(); FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Filter = "*.*"; watcher.Deleted += new FileSystemEventHandler(watcher_Deleted); watcher.Created += watcher_FileCreated; watcher.Path = path; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; } private void ProcessQueue() { try { Console.WriteLine("Processing queue, " + filePaths.Count + " files created:"); rwlock.EnterReadLock(); } finally { if (processTimer != null) { processTimer.Stop(); processTimer.Dispose(); processTimer = null; OnFileListCreated(new FileListEventArgs { FileList = filePaths }); filePaths.Clear(); } rwlock.ExitReadLock(); } } private void ProcessDeletionQueue() { try { Console.WriteLine("Processing queue, " + deletedFilePaths.Count + " files created:"); rwlock.EnterReadLock(); } finally { if (processTimer != null) { processTimer.Stop(); processTimer.Dispose(); processTimer = null; OnFileListDeleted(new FileListEventArgs { FileList = deletedFilePaths }); deletedFilePaths.Clear(); } rwlock.ExitReadLock(); } } void watcher_FileCreated(object sender, FileSystemEventArgs e) { try { rwlock.EnterWriteLock(); filePaths.Add(e.FullPath); if (processTimer == null) { // First file, start timer. processTimer = new Timer(2000); processTimer.Elapsed += (o, ee) => ProcessQueue(); processTimer.Start(); } else { // Subsequent file, reset timer. processTimer.Stop(); processTimer.Start(); } } finally { rwlock.ExitWriteLock(); } } void watcher_Deleted(object sender, FileSystemEventArgs e) { try { rwlock.EnterWriteLock(); deletedFilePaths.Add(e.FullPath); if (deletionTimer == null) { // First file, start timer. deletionTimer = new Timer(2000); deletionTimer.Elapsed += (o, ee) => ProcessDeletionQueue(); deletionTimer.Start(); } else { // Subsequent file, reset timer. deletionTimer.Stop(); deletionTimer.Start(); } } finally { rwlock.ExitWriteLock(); } } 

So, how do I do this to get the original path where the files were, as well as a new path to where they were moved? (See the first question, why does the timer code exist to pause event processing until all files are moved in multi-point move).

+1
source share
2 answers

You declare two timers, but use only one of them (you use the same method in the process delete method). So first, we start with a simple copy / paste error.

+1
source

I found that FileSystemWatcher events are NOT queued (caveat: this was about 8 years ago).

If you process a file and create 3 new files during processing, you will not receive 3 events.

After processing the file, you may need to manually check the directory for other changes.

0
source

Source: https://habr.com/ru/post/894299/


All Articles