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. FileWatcher

FileWatcher

Scheduled Pinned Locked Moved C#
questioncsharpc++xml
2 Posts 2 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.
  • L Offline
    L Offline
    LCI
    wrote on last edited by
    #1

    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?

    V 1 Reply Last reply
    0
    • L LCI

      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?

      V Offline
      V Offline
      vineas
      wrote on last edited by
      #2

      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.

      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