我使用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;
}
答案 0 :(得分:1)
一种方法是枚举所有目录,并在每个目录上使用FileSystemWatcher
观看所有目录。
但它会消耗大量资源。因此,您可以另外查看此链接:Filewatcher for the whole computer (alternative?)
答案 1 :(得分:1)
您可以使用IncludeSubdirectories到逻辑驱动器来观看整个系统。 试试这段代码,
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;
}