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 -