Handling File Events in C#
-
Hello. I am new to C# (used Java originally), and I was wondering what would be the best way to handle file events in a console application. Specifically, I need to monitor a bunch of directories to see if new files are being added, and then do something with them if so. I guess I'm confused because I need to thread the watchers, but am unsure how/where to handle their corresponding events. I have been looking around for resources but am coming up on threading samples and event samples but not any combination of the two and so I don't know exactly where to start. If anybody has a suggestion or resource, I would greatly appreciate it!!
-
Hello. I am new to C# (used Java originally), and I was wondering what would be the best way to handle file events in a console application. Specifically, I need to monitor a bunch of directories to see if new files are being added, and then do something with them if so. I guess I'm confused because I need to thread the watchers, but am unsure how/where to handle their corresponding events. I have been looking around for resources but am coming up on threading samples and event samples but not any combination of the two and so I don't know exactly where to start. If anybody has a suggestion or resource, I would greatly appreciate it!!
start by reading about "System.IO.FileSystemWatcher".
-
start by reading about "System.IO.FileSystemWatcher".
Thank you, that helped clarify some of the event concepts for me. I guess I'm wondering now whether the filesystemwatcher acts as a thread... meaning does it listen indefinitely while the main program works until it hears something? Or does it have to be wrapped in a thread to do so?
-
Thank you, that helped clarify some of the event concepts for me. I guess I'm wondering now whether the filesystemwatcher acts as a thread... meaning does it listen indefinitely while the main program works until it hears something? Or does it have to be wrapped in a thread to do so?
I believe it works on its own thread, then invokes the events to the UI thread. If your GUI is going to stay responsive, you don't have to worry about threading at all. Just create it, set the needed properties (Path, NotifyFilter, Filter), turn on EnableRaisingEvents, hook the events you want, and handle them as needed. If I remember right, this is a port from the old VBScript library, so it's intended to be as easy to use as possible. To make sure it works that way, you might want to just be sure that you're CREATING it on the UI thread. If you don't, it might invoke events to a different thread unless you set the SynchronizingObject property to a GUI control. You can also use that same property to invoke the events onto a thread of your choosing. If you want finer control, you can skip the events entirely, create it on a dedicated background thread, and loop on the WaitForChanged method, which will block until it receives a notification. Most of the time, this is overkill. There's more detail on each of those properties and methods in the MSDN help. EDIT: Sorry, missed where you said it was a console application. In that case, you probably want to go with the last method I mentioned, looping on WaitForChanged to do everything synchronously.
modified on Wednesday, March 25, 2009 11:10 AM