简体   繁体   中英

C# - File Watcher as a windows service watching multiple directories

I have a problem that I can't seem to find a solution for. I am writing a file watcher Windows Service in C# using FileSystemWatcher. I would like to watch multiple independent directories, I have the directories to watch and some other settings in a xml file. The program reads in the xml, creates the appropriate number of FileSystemWatchers and stores these system watchers in a dictionary with a randomly generated id number (as the keys). The problem that I am having is that only the first FileSystemWatcher is responding to files being created/deleted/updated ect. I know the others have been create because the entries are in the dictionary. I know EnableRaisingEvents = true because all of the FileSystemWatcher are created the same way via a loop. I have no idea how to get multiple file watchers working correctly, and I would like to avoid threads if possible. If anyone has any ideas or resources to look at, that would be greatly appreciated.

Im afraight you háve to instantiate some kind of thread pool or task factory because a filewatcher can only watch one file in a thread.

However this posting has a very nice example of doing so: How to assign separate thread for each File System Watcher?

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM