Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 0.92 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using multiple FileSystemWatchers
  2. private static FileSystemWatcher watcherTxt;
  3. private static FileSystemWatcher watcherXml;
  4.  
  5. static void Main(string[] args)
  6. {
  7.     String dir = @"C:temp";
  8.  
  9.     watcherTxt = new FileSystemWatcher();
  10.     watcherTxt.Path = dir;
  11.     watcherTxt.Filter = "*.txt";
  12.     watcherTxt.EnableRaisingEvents = true;
  13.     watcherTxt.Created += new FileSystemEventHandler(onCreatedFile);
  14.  
  15.     watcherXml = new FileSystemWatcher();
  16.     watcherXml.Path = dir;
  17.     watcherXml.Filter = "*.xml";
  18.     watcherXml.EnableRaisingEvents = true;
  19.     watcherXml.Created += new FileSystemEventHandler(onCreatedFile);
  20.  
  21.     Console.ReadLine();
  22. }
  23.  
  24. private static void onCreatedFile(object sender, FileSystemEventArgs e)
  25. {
  26.     if (watcherTxt == sender)
  27.     {
  28.         Console.WriteLine("Text Watcher Detected: " + e.FullPath);
  29.     }
  30.  
  31.     if (watcherXml == sender)
  32.     {
  33.         Console.WriteLine("XML Watcher Detected: " + e.FullPath);
  34.     }
  35. }