Search code examples
c#filesystemwatcher

C# FileSystem Watcher Not raising Error Event - had to poll the directory to trigger it


We are using FileSystemwatcher to look for certain files in a directory and it goes down sometimes. We have handled the file system watcher's Error event and restart the watcher gracefully as mentioned in the question here -> FileSystemWatcher stops catching events

But on several occasions, the network connectivity of the server has gone down and the error event was never raised. So, we used a small method to keep polling the directory every 3 seconds using

if(!Directory.Exists(NetWorkPath)){
Logger.Log("Error! The directory doesn't exist");
}

and this seems FSW to trigger the Error event correctly.

Questions -

  • Is this the correct way of doing this?
  • Polling the directory every 3 (or any arbitrary 'x' seconds) to see if it still exists defeats the purpose of using the FSW, I could instead use the polling method to look for the new files and handle it accordingly.
  • I have no clear indication anywhere that this is the expected behavior of FSW, so, could it be a framework 4.0 bug?

Note: We came this current solution based on these articles -


Solution

  • Using a FileSystemWatcher on a network drive (I am assuming that is what you are doing given the network connectivity mention) is a risky proposition at best. Even with SANs and local drives events can be lost if the volume of files being created / deleted is high enough and on a regular network drive it is even more unreliable.

    Implementing a polling solution, crude as it may seem, would be a more reliable solution.