FileSystemWatch firing wrong event!!!

Hi,

I have experimeneted several problems with FileSystemWatcher which are explained on this forum, such as multiple events or events firing too soon.

I'm monitoring 2 directories with 2 FileSystemWatcher objects for specific (and different) filenames, one of the directory is a sub of the other. I monitor both changed and created events. The problem is that when the file being monitored in the parent directory gets created the event handler associated to the file in the subdirectoy also gets fired. Note that in both cases I do set the IncludeSubdirectories attribute to false.

Of course I can handle this by verifying the file name at the beginning of each event handler but still it is annoying (and caused me a fair bit of pain before I figured out what appened).

This is what I do (in distinct methods) for both directories:

//Create a new FileSystemWatcher and set its properties.
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = dirToWatch;
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
fileSystemWatcher.IncludeSubdirectories = false;

string fileName;
fileName = "whatever.txt";
fileSystemWatcher.Filter = fileName;

// Add event handlers for creation and modification.
fileSystemWatcher.Changed += new FileSystemEventHandler(OnChanged);
fileSystemWatcher.Created += new FileSystemEventHandler(OnChanged);

//Begin watching.
fileSystemWatcher.EnableRaisingEvents = true;












Answer this question

FileSystemWatch firing wrong event!!!

  • Michelle A.

    Well ok, but we already know FSW is quite tricky to use, first is may generate the same event multiple times but also it can generate an event before the file is released by the application that created it. So it is not a giant leap to imagine there may be other tricky "features".

    Now when I dump a file manually in one folder but it triggers 2 distinct event handlers that are supposed to monitor different directories and filter different filenames.





  • 2162

    What event handler handles the notifications from the 2nd watcher OnChanged Just checking...


  • Dhaval Mistry

    Don't debug this assuming it might be an FSW problem. FSW is *very* popular and was popular way back before the framework made it easy to use. The IncludeSubDirectories property not working right would have been caught a long time ago. On possible reason that jumps to mind is that the app you're monitoring is changing files in both folders. Note that FSW makes no guarantee that notifications are delivered in order or timely.


  • Tryin2Bgood

    The second event handler is a different one (OnchangeAlt) and is setup the same way is a different method.

    I think this could be due to the fact that the two monitored directories have a parent-child relationship.

    I will post if this is indeed the problem, in which case it would be a Microsoft bug.


  • Emadkb

    Mea culpa!!!

    Nasty bug in our software caused this.

  • FileSystemWatch firing wrong event!!!