C#: Using FileSystemWatcher to watch for changes to files

♀尐吖头ヾ 提交于 2019-12-02 01:35:32

It's enough if you create a watcher for each directory (and optionally, you can have the watcher to monitor a whole directory tree.) You can then use the events to compare the changed files with the list of files you are interested in.

I would suggest you make some kind of "nanny" class for the watchers to ensure you doesn't dispose active watchers, or create duplicate. Just a tip :)

Btw, yes, there's a limit, you can't create infinite watchers. In specific scenarios that can be a problem, but most likely, that's not the case for you

I think you're definitely on the right track. It's up to you how to store the multiple fileWatchers but disposing of them and clearing their queue as quickly as possible are important for reliability. You only need one per directory, not file.

There are lots of posts about the unreliability of fileWatchers so be diligent in your usage.

This really is a trade-off. Many watchers will consume more resources, but if you have a watcher on a directory containing many files/sub directories (e.g. C:), this will also consume a lot of resources.

If all the files are located in a single folder, you should just use the single watcher.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!