2011-07-13 77 views
1

目前我有一個Windows服務,它不斷監視4個文件夾。我已經使用FileSystemWatchers來監視文件夾。C#中的文件系統監視器

但是每次添加新文件夾時,我都要卸載服務添加一個新的filesystemwatcher然後安裝服務。

我想讓它動態或數據庫驅動,我不需要卸載並重新安裝服務,每次程序需要監視一個新的文件夾。

我該如何做到這一點?

在此先感謝。

回答

0

在這裏,你有一個使用XML與Linq2XML和擁有的配置文件更改支票演示:

class Program 
{ 
    static List<FileSystemWatcher> _watchers = new List<FileSystemWatcher>(); 
    static bool _shouldReload = false; 

    static void WaitReady(string fileName) 
    { 
     while (true) 
     { 
      try 
      { 
       using (Stream stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) 
       { 
        if (stream != null) 
        { 
         System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} ready.", fileName)); 
         break; 
        } 
       } 
      } 
      catch (FileNotFoundException ex) 
      { 
       System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); 
      } 
      catch (IOException ex) 
      { 
       System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); 
      } 
      catch (UnauthorizedAccessException ex) 
      { 
       System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message)); 
      } 
      Thread.Sleep(500); 
     } 
    } 

    static void Main(string[] args) 
    { 
     var configWatcher = new FileSystemWatcher(Path.GetDirectoryName(Path.GetFullPath("config.xml")), "config.xml"); 
     configWatcher.Changed += (o, e) => 
     { 
      lock (_watchers) 
      { 
       _watchers.ForEach(w => { w.EnableRaisingEvents = false; w.Dispose(); }); 
       _watchers.Clear(); 
      } 

      _shouldReload = true; 
     }; 

     configWatcher.EnableRaisingEvents = true; 

     Thread t = new Thread((ThreadStart)(() => { 
      while (true) 
      { 
       Thread.Sleep(5000); // reload only every five seconds (safety measure) 
       if (_shouldReload) 
       { 
        _shouldReload = false; 
        Console.WriteLine("Reloading configuration."); 
        WaitReady(Path.GetFullPath("config.xml")); 
        loadConfigAndRun(); 
       } 
      } 
     })); 

     t.IsBackground = true; 
     t.Start(); 

     loadConfigAndRun(); 

     Console.ReadLine(); 
    } 

    static void loadConfigAndRun() 
    { 
     var config = XElement.Load("config.xml").Elements(); 
     var paths = from watcher in config select watcher.Attribute("Folder").Value; 

     foreach (var path in paths) 
     { 
      var watcher = new FileSystemWatcher(path); 
      watcher.Created += new FileSystemEventHandler(watcher_Changed); 
      watcher.Changed += new FileSystemEventHandler(watcher_Changed); 
      watcher.Deleted += new FileSystemEventHandler(watcher_Changed); 
      watcher.Renamed += new RenamedEventHandler(watcher_Renamed); 
      watcher.EnableRaisingEvents = true; 
      _watchers.Add(watcher); 
     } 
    } 

    static void toggleWatcher(string path) 
    { 
     var watcher = _watchers.FirstOrDefault(w => w.Path == path); 

     if (watcher != null) 
     { 
      watcher.EnableRaisingEvents = !watcher.EnableRaisingEvents; 
     } 
    } 

    static void watcher_Renamed(object sender, RenamedEventArgs e) 
    { 
     var watcher = sender as FileSystemWatcher; 

     Console.WriteLine("Something renamed in " + watcher.Path); 
    } 

    static void watcher_Changed(object sender, FileSystemEventArgs e) 
    { 
     var watcher = sender as FileSystemWatcher; 

     Console.WriteLine("Something changed in " + watcher.Path); 
    } 
} 

這是config.xml文件:

<?xml version="1.0" encoding="utf-8" ?> 
<Watchers> 
    <Watcher Folder="d:\ktm"></Watcher> 
    <Watcher Folder="c:\windows"></Watcher> 
</Watchers> 
3

您可以使用應用程序定期讀取和緩存新文件夾的XML配置文件。這些將在app.settings文件中進行。

<configuration> 
    <appSettings> 
     <add key = "FolderToWatch" value = "C:\SomeFolder\" /> 
     <add key = "FolderToWatch" value = "C:\SomeFolder\AnotherOne\" /> 
     <add key = "FolderToWatch" value = "D:\LastOne\" /> 
    </appSettings> 
</configuration> 
+0

通過21秒打我。 .. – AllenG

1

使用配置文件添加您的新位置。然後,只需要在將新位置添加到配置文件時重新啓動服務即可。

0

我會使用.YML建議的.xml文件,但是,我只會看.xml文件。服務啓動時加載.xml文件,然後監視.xml文件以進行更改;如果LastWriteTime的.xml文件發生更改,請重新加載它。