2010-04-26 154 views

回答

19

我不認爲FSW支持監視多個文件夾,因此只需爲要監視的每個文件夾實例化一個文件夾。儘管如此,你可以用相同的方法指出事件處理程序,最終應該像我想的那樣工作。

0

你可以簡單地使用FileSystemWatcher的多個實例,每個目錄一個嗎?

12

最簡單的方法是創建FileSystemWatcher對象的多個實例。

http://www.c-sharpcorner.com/UploadFile/mokhtarb2005/FSWatcherMB12052005063103AM/FSWatcherMB.aspx

你必須確保你的兩個文件夾之間正確地處理事件:

雖然一些常見的occurances,例如 如複製或移動文件時,不要 對應直接發生事件,這些事件的發生確實導致 引發。當您複製文件時, 系統會在複製文件的 目錄中引發Created事件 ,但不會在 原始目錄中引發任何事件。當您移動 文件時,服務器會引發兩個事件: 源目錄中的已刪除事件 後跟 目標目錄中的Created事件。

例如,您創建FileSystemWatcher的兩個實例 。 FileSystemWatcher1設置爲觀看 「C:\ My Documents」,並且 FileSystemWatcher2設置爲觀看 「C:\ Your Documents」。現在,如果將 文件從「我的文檔」複製到「您的 文檔」中,FileSystemWatcher2引發的創建事件將爲 ,但 事件將引發 FileSystemWatcher1。與複製不同,移動文件或目錄的 會引起兩個事件。從前面的例子中, 如果你感動從「我的 的文檔」文件,以「您的文檔」,一個 創建活動將通過 FileSystemWatcher2提高和刪除事件 將由FileSystemWatcher的

鏈接提高到FileSystemEventArgs

+0

這樣我就可以用同樣的方法來處理這兩個目錄,也就是說,作爲下面的例子中: fileWatcher [指數] .Created + =新FileSystemEventHandler(OnCreated); 在上述情況下,OnCreated()如何知道索引值(或者需要監視的目錄)?謝謝。 – 2010-04-26 20:04:37

+0

@Bi如果我明白你在問什麼(漫長的一天)。導演信息將作爲FileSystemEventArs參數的一部分傳遞給OnCreated函數。 http://msdn.microsoft.com/en-us/library/system.io.filesystemeventargs.aspx – kemiller2002 2010-04-26 20:08:10

+0

完美 - 謝謝! – 2010-04-26 20:09:14

3

開箱即用,FileSystemWatcher僅支持監視單個父目錄。要監視多個同級目錄,您需要創建FileSystemWatcher的多個實例。

但是,您可以嘗試欺騙此行爲,方法是利用FileSystemWatcher包含子目錄的功能。您可以從您正在觀看的目錄中創建一個NTFS交接點(又名符號鏈接)作爲子目錄。 Sysinternals成名的Mark Russinovich有一個名爲Junction的工具來簡化符號鏈接的創建和管理。

請注意,您只能創建符號鏈接到本地​​計算機上的目錄。

1

您將不得不實例化FileSystemWatcher對象的多個實例。雖然可以將事件綁定到相同的方法,並使用發件人對象來確定哪個觸發事件。

 var fsw1 = new FileSystemWatcher(); 
     var fsw2 = new FileSystemWatcher(); 
     FileSystemEventHandler fsw_changed = delegate(object sender, FileSystemEventArgs e) 
     { 
      Console.WriteLine("{0} - {1}", (sender as FileSystemWatcher).Path, e.ChangeType); 
     }; 
     fsw1.Changed += fsw_changed; 
     fsw2.Changed += fsw_changed; 
0

,或者您可以通過路徑中碼,以紀念在一定的範圍在這看着像域:

multiple monitor link

希望這會有所幫助。

+0

您指向的鏈接爲每個路徑創建一個新的fsw對象。不是什麼操作要求。 – 2018-02-22 21:25:00

2

儘管這是一個老問題,我決定回答,因爲我無法在任何地方找到好答案。

因此,目的是使用FileSystemWatcher監視多個文件夾(而不是子目錄)?這裏是我的建議:

using System; 
using System.IO; 
using System.Security.Permissions; 
using System.Collections.Generic; 

namespace MultiWatcher 
// ConsoleApplication, which monitors TXT-files in multiple folders. 
// Inspired by: 
// http://msdn.microsoft.com/en-us/library/system.io.filesystemeventargs(v=vs.100).aspx 

{ 
    public class Watchers 
    { 
     public static void Main() 
     { 
      Run(); 

     } 

     [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] 
     public static void Run() 
     { 
      string[] args = System.Environment.GetCommandLineArgs(); 

      // If a directory is not specified, exit program. 
      if (args.Length < 2) 
      { 
       // Display the proper way to call the program. 
       Console.WriteLine("Usage: Watcher.exe PATH [...] [PATH]"; 
       return; 
      } 
      List<string> list = new List<string>(); 
      for (int i = 1; i < args.Length; i++) 
      { 
       list.Add(args[i]); 
      } 
      foreach (string my_path in list) 
      { 
       Watch(my_path); 
      } 

      // Wait for the user to quit the program. 
      Console.WriteLine("Press \'q\' to quit the sample."); 
      while (Console.Read() != 'q') ; 
     } 
     private static void Watch(string watch_folder) 
     { 
      // Create a new FileSystemWatcher and set its properties. 
      FileSystemWatcher watcher = new FileSystemWatcher(); 
      watcher.Path = watch_folder; 
      /* Watch for changes in LastAccess and LastWrite times, and 
       the renaming of files or directories. */ 
      watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
       | NotifyFilters.FileName | NotifyFilters.DirectoryName; 
      // Only watch text files. 
      watcher.Filter = "*.txt"; 

      // Add event handlers. 
      watcher.Changed += new FileSystemEventHandler(OnChanged); 
      watcher.Created += new FileSystemEventHandler(OnChanged); 
      watcher.Deleted += new FileSystemEventHandler(OnChanged); 
      watcher.Renamed += new RenamedEventHandler(OnRenamed); 

      // Begin watching. 
      watcher.EnableRaisingEvents = true; 
     } 

     // Define the event handlers. 
     private static void OnChanged(object source, FileSystemEventArgs e) 
     { 
      // Specify what is done when a file is changed, created, or deleted. 
      Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); 
     } 

     private static void OnRenamed(object source, RenamedEventArgs e) 
     { 
      // Specify what is done when a file is renamed. 
      Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); 
     } 
    } 
} 
+3

FileWatcher實現IDisposable。如果在程序退出之前不再需要觀察者(例如,如果特定的觀察目錄被刪除),那麼通過包括確保IDisposable.Dispose()的模式來改進您的建議。 – 2013-02-02 20:34:11