2

FileSystemWatcher用于监视文件系统。它可以在特定的文件夹或驱动器上观看。

但我希望它在整个文件系统上意味着它应该在所有驱动器上观看。

对此有任何想法吗?

我做那么多。

public static void Run()
{
     string[] args = System.Environment.GetCommandLineArgs();

     if (args.Length < 2)
     {
          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)
     {
          WatchFile(my_path);
     }

     Console.WriteLine("Press \'q\' to quit the sample.");
     while (Console.Read() != 'q') ;
}
private static void WatchFile(string watch_folder)
{
    watcher.Path = watch_folder;

    watcher.NotifyFilter = NotifyFilters.LastWrite;
    watcher.Filter = "*.xml";
    watcher.Changed += new FileSystemEventHandler(convert);
    watcher.EnableRaisingEvents = true;
}

使用Filesystem Watcher - 多个文件夹

4

2 回答 2

3

一种方法是枚举所有目录并通过FileSystemWatcher在每个目录上使用来查看所有目录。

但是会消耗大量资源。因此,您可以换个方式查看此链接:Filewatcher for the entire computer (alternative?)

于 2013-04-10T11:04:00.097 回答
2

您可以使用 IncludeSubdirectories to Logical Drives 来监视整个系统。试试这个代码,

string[] drives = Environment.GetLogicalDrives();

foreach(string drive in drives)
{
   FileSystemWatcher watcher = new FileSystemWatcher();
   watcher.Path = drive;
   watcher.IncludeSubdirectories = true;
   watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                   | NotifyFilters.FileName | NotifyFilters.DirectoryName;

   watcher.Filter = "*.txt";

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

   watcher.EnableRaisingEvents = true;
}
于 2016-06-28T20:18:01.863 回答