使用FileSystemWatcher监视多个文件夹

使用FileSystemWatcher监视多个文件夹

问题描述:

在C#中使用FileSystemWatcher监视多个文件夹(而不是子目录)的最佳方式是什么?使用FileSystemWatcher监视多个文件夹

我不认为FSW支持监视多个文件夹,因此只需为要监视的每个文件夹实例化一个文件夹。尽管如此,你可以用相同的方法指出事件处理程序,最终应该像我想的那样工作。

你可以简单地使用FileSystemWatcher的多个实例,每个目录一个吗?

最简单的方法是创建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

开箱即用,FileSystemWatcher仅支持监视单个父目录。要监视多个同级目录,您需要创建FileSystemWatcher的多个实例。

但是,您可以尝试欺骗此行为,方法是利用FileSystemWatcher包含子目录的功能。您可以从您正在观看的目录中创建一个NTFS交接点(又名符号链接)作为子目录。 Sysinternals成名的Mark Russinovich有一个名为Junction的工具来简化符号链接的创建和管理。

请注意,您只能创建符号链接到本地​​计算机上的目录。

您将不得不实例化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; 

,或者您可以通过路径中码,以纪念在一定的范围在这看着像域:

multiple monitor link

希望这会有所帮助。

+0

您指向的链接为每个路径创建一个新的fsw对象。不是什么操作要求。 – 2018-02-22 21:25:00

尽管这是一个老问题,我决定回答,因为我无法在任何地方找到好答案。

因此,目的是使用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