FileWatcher
-
I use File Watcher to monitor a folder for any new files. The intention is that when an XML file gets dropped in that folder, filewatcher will send an event and the event handler will take care of it. But how do i get the event handler to now to read all the files in that directory. So if 10 XML files get dumped in there. i am assuming that in the event handler i have to use something like FindFirstFile in C++ to actually load the files regradless of name. DOes C# have an equivalent or is there a better way to do this?
-
I use File Watcher to monitor a folder for any new files. The intention is that when an XML file gets dropped in that folder, filewatcher will send an event and the event handler will take care of it. But how do i get the event handler to now to read all the files in that directory. So if 10 XML files get dumped in there. i am assuming that in the event handler i have to use something like FindFirstFile in C++ to actually load the files regradless of name. DOes C# have an equivalent or is there a better way to do this?
If 10 files get dropped there, you should have 10 calls to your handler. I can't remember exactly (it's been a while), but I'm pretty sure the calls come back via the thread pool so even if you wanted to process them all from one of the handler calls, you'd have to make sure that only one of the calls is actually processing the files, and not the others, with a lock or mutex of some sort. If this is for sure how you want to handle it (process from a single handler call), you could do something like this (after first making sure the code is only running on one of the handlers).
void watcher_Changed(object sender, FileSystemEventArgs e) { // TODO: Make sure that this is the only handler running on these files string[] fileList = Directory.GetFiles(Path.GetDirectoryName(e.FullPath), "*.xml"); foreach (string file in fileList) // do the processing }
----- In the land of the blind, the one eyed man is king.