What's the best practice to recover from a FileSystemWatcher error?

.net, c#, exception, filesystemwatcher

Solution

Depends on the error surely?

- If it is too much data because the buffer was overrun (many changes) do a list directory and grab the changes you're after.

- If it is too much data because you're not processing the FileSystemWatcher events quickly enough, ensure you're processing it efficiently.

- Deleted directory, can't do anything about it other than disposing the FileSystemWatcher, or maybe watching the parent for a recreation of that directory name again.

Problem

After a `FileSystemWatcher.Error` event was raised, I have no clue about what to do next. The exception can be a [relatively] minor one, such as too many changes at once in directory which doesn't affect the watcher's watching process, but it can also be a big issue - such as the watched directory being deleted, in which case the watcher is no longer functional. My question is what is the best way to handle the Error event?

Original source