Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Handling File Events in C#

Handling File Events in C#

Scheduled Pinned Locked Moved C#
csharpjavalearning
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    agent00zelda
    wrote on last edited by
    #1

    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!!

    T 1 Reply Last reply
    0
    • A agent00zelda

      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!!

      T Offline
      T Offline
      Thomas Krojer
      wrote on last edited by
      #2

      start by reading about "System.IO.FileSystemWatcher".

      A 1 Reply Last reply
      0
      • T Thomas Krojer

        start by reading about "System.IO.FileSystemWatcher".

        A Offline
        A Offline
        agent00zelda
        wrote on last edited by
        #3

        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 1 Reply Last reply
        0
        • A agent00zelda

          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 Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups