监控d盘下的所有.txt文件的修改
{
FileSystemWatcher watcher = new FileSystemWatcher();
try
{
watcher.Path = @"d:\Test";
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
return;
}
//设置监视文件的哪些修改行为
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;
Console.WriteLine(@"Press 'q' to quit app.");
while (Console.Read() != 'q');
}
static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File:{0} {1}!", e.FullPath, e.ChangeType);
}
static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("File:{0} renamed to\n{1}", e.OldFullPath, e.FullPath);
}
-----------------------------------
需要进技术群交流的,请添加小编zls20210502!