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) {
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).
source share