使用FileSystemWatcher监视多个文件夹

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

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

最简单的方法是创建FileSystemWatcher对象的多个实例。

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

您必须确保正确处理两个文件夹之间的事件:

尽管一些常见的事件(例如复制或移动文件)并不直接与事件相对应,但这些事件确实会引发事件。 复制文件时,系统会在复制文件的目录中引发Created事件,但不会引发原始目录中的任何事件。 移动文件时,服务器会引发两个事件:源目录中的Deleted事件,然后是目标目录中的Created事件。

例如,您创建两个FileSystemWatcher实例。 FileSystemWatcher1设置为观看“C:\ My Documents”,FileSystemWatcher2设置为观看“C:\ Your Documents”。 现在,如果将文件从“我的文档”复制到“您的文档”中,FileSystemWatcher2将引发Created事件,但不会为FileSystemWatcher1引发任何事件。 与复制不同,移动文件或目录会引发两个事件。 在上一个示例中,如果您将文件从“我的文档”移动到“您的文档”,则FileSystemWatcher2将引发Created事件,FileSystemWatcher将引发Deleted事件。

链接到FileSystemEventArgs

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

但是,您可以尝试通过利用FileSystemWatcher包含子目录的function来欺骗此行为。 您可以创建一个NTFS交接点(也称为符号链接)作为您正在观看的目录的子目录。 Sysinternals成名的Mark Russinovich有一个名为Junction的实用程序,用于简化符号链接的创建和管理。

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

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

那么,目的是使用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 list = new List(); 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); } } } 

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

您必须实例化FileSystemWatcher对象的多个实例。 虽然您可以将事件绑定到同一个方法并使用sender对象来确定哪个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; 

或者您可以在代码中传递路径,以标记在此处观看的某个范围的域:

多个监视器链接

希望这可以帮助。