usingSystem;usingSystem.IO;classProgram{staticvoidMain(string[]args){// 创建一个新的FileSystemWatcher对象FileSystemWatcherwatcher=newFileSystemWatcher();// 设置要监视的目录路径watcher.Path=@"C:\MyFolder";// 设置要监视的文件和文件夹更改类型watcher.NotifyFilter=NotifyFilters.LastWrite|NotifyFilters.FileName|NotifyFilters.DirectoryName;// 只监视文本文件类型watcher.Filter="*.txt";// 添加事件处理程序watcher.Changed+=newFileSystemEventHandler(OnChanged);watcher.Created+=newFileSystemEventHandler(OnCreated);watcher.Deleted+=newFileSystemEventHandler(OnDeleted);watcher.Renamed+=newRenamedEventHandler(OnRenamed);// 开始监视watcher.EnableRaisingEvents=true;// 等待用户输入Console.WriteLine("Press 'q' to quit the sample.");while(Console.Read()!='q');}// 文件或文件夹更改事件处理程序privatestaticvoidOnChanged(objectsource,FileSystemEventArgse){Console.WriteLine($"File {e.FullPath} has been changed.");}// 文件或文件夹创建事件处理程序privatestaticvoidOnCreated(objectsource,FileSystemEventArgse){Console.WriteLine($"File {e.FullPath} has been created.");}// 文件或文件夹删除事件处理程序privatestaticvoidOnDeleted(objectsource,FileSystemEventArgse){Console.WriteLine($"File {e.FullPath} has been deleted.");}// 文件或文件夹重命名事件处理程序privatestaticvoidOnRenamed(objectsource,RenamedEventArgse){Console.WriteLine($"File {e.OldFullPath} has been renamed to {e.FullPath}");}}