I need to setup an application that watches for files being created in a directory, both locally or on a network drive.
Would the FileSystemWatcher
or polling on a timer would be the best option. I have used both methods in the past, but not extensively.
What issues (performance, reliability etc.) are there with either method?
Working solution for working with create event instead of change
Even for copy, cut, paste, move.
Solution for this file watcher while file attribute change event using static storage
This is a workaround solution for this problem of multiple triggering event.
I currently use the
FileSystemWatcher
on an XML file being updated on average every 100 milliseconds.I have found that as long as the
FileSystemWatcher
is properly configured you should never have problems with local files.I have no experience on remote file watching and non-Windows shares.
I would consider polling the file to be redundant and not worth the overhead unless you inherently distrust the
FileSystemWatcher
or have directly experienced the limitations everyone else here has listed (non-Windows shares, and remote file watching).Personally, I've used the
FileSystemWatcher
on a production system, and it has worked fine. In the past 6 months, it hasn't had a single hiccup running 24x7. It is monitoring a single local folder (which is shared). We have a relatively small number of file operations that it has to handle (10 events fired per day). It's not something I've ever had to worry about. I'd use it again if I had to remake the decision.I'd go with polling.
Network issues cause the
FileSystemWatcher
to be unreliable (even when overloading the error event).I would say use polling, especially in a TDD scenario, as it is much easier to mock/stub the presence of files or otherwise when the polling event is triggered than to rely on the more "uncontrolled" fsw event. + to that having worked on a number of apps which were plagued by fsw errors.
I had some big problems with FSW on network drives: Deleting a file always threw the error event, never the deleted event. I did not find a solution, so I now avoid the FSW and use polling.
Creation events on the other hand worked fine, so if you only need to watch for file creation, you can go for the FSW.
Also, I had no problems at all on local folders, no matter if shared or not.