C# 使用FileSystemWatcher监视文件系统
我使用C# 使用FileSystemWatcher监视文件系统,c#,C#,我使用FileSystemWatcher来监视文件系统。它可以监视特定文件夹或驱动器 但我希望它在整个文件系统上,这意味着它应该在所有驱动器上进行监视 你知道吗 我经常这么做 public static void Run() { string[] args = System.Environment.GetCommandLineArgs(); if (args.Length < 2) { Console.WriteLine("Usage:
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;
}
publicstaticvoidrun()
{
字符串[]args=System.Environment.GetCommandLineArgs();
如果(参数长度<2)
{
WriteLine(“用法:Watcher.exe路径[…][PATH]”;
回来
}
列表=新列表();
for(int i=1;i
使用一种方法是枚举所有目录,并通过在每个目录上使用
FileSystemWatcher
来监视所有目录
但是它会消耗大量的资源。因此,您可以查看此链接:一种方法是枚举所有目录,并通过在每个目录上使用
FileSystemWatcher
来监视所有目录
但是它会消耗大量的资源。因此,您可以查看此链接:您可以使用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;
}
您可以使用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;
}
您是指所有
root
文件夹或所有文件夹和子文件夹(递归)?您是指所有root
文件夹或所有文件夹和子文件夹(递归)?