c# - 使用 FileSystemWatcher 监视文件系统

标签 c#

我使用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 - Multiple folders

最佳答案

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

但是会消耗大量资源。因此,您可以替代查看此链接:Filewatcher for the whole computer (alternative?)

关于c# - 使用 FileSystemWatcher 监视文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15924025/

相关文章:

C# 使用自定义对象扩展包含列的 listView

c# - 重命名为带波浪符号的文件夹

c# - 将一个数字拆分成多个数字

c# - 如何知道哪个 FileSystemWatcher 正在调用方法?

c# - 单元测试中的静态类/方法/属性,停止与否

c# - 如何从 Windows 应用商店应用程序中的视频获取缩略图?

c# - 如何生成随机字母数字字符串?

c# - 自动异步套接字测试

c# - 具有非常慢的 CreateViewStream 的 MemoryMappedFile

c# - 带有 Angular Postback 的 ASP.NET WebForms