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

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 に答える