C#-使用FileSystemWatcher類觀察監測系統文件和目錄的更改
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
FileSystemWatcher類FileSystemWatcher類的主要功能: 觀察文件的更改使用FileSystemWatcher可以監視文件的更改。事件在創建、重命名、刪除和更改文件時觸發。這可用于如下場景:需要對文件的變更作出反應。例如,服務器上傳文件時,或文件緩存在內存中,而緩存需要在文件更改時失效。 using System; using System.IO; namespace FileMonitor { public class Program { private static FileSystemWatcher s_watcher; public static void Main(string[] args) { // 監視當前目錄下所有的txt文件 WatchFiles(".", "*.txt"); Console.ReadLine(); UnWatchFiles(); } public static void WatchFiles(string path, string filter) { s_watcher = new FileSystemWatcher(path, filter) { IncludeSubdirectories = true }; s_watcher.Created += OnFileChanged; s_watcher.Changed += OnFileChanged; s_watcher.Deleted += OnFileChanged; s_watcher.Renamed += OnFileRenamed; // 啟用事件的觸發,這一點非常重要 s_watcher.EnableRaisingEvents = true; Console.WriteLine("watching file changes..."); } public static void UnWatchFiles() { s_watcher.Created -= OnFileChanged; s_watcher.Changed -= OnFileChanged; s_watcher.Deleted -= OnFileChanged; s_watcher.Renamed -= OnFileRenamed; s_watcher.Dispose(); s_watcher = null; } private static void OnFileRenamed(object sender, RenamedEventArgs e) => Console.WriteLine($"file {e.OldName} {e.ChangeType} to {e.Name}"); private static void OnFileChanged(object sender, FileSystemEventArgs e) => Console.WriteLine($"file {e.Name} {e.ChangeType}"); } } 微軟官方示例程序The following example creates a FileSystemWatcher to watch the directory specified at run time. The component is set to watch for changes in LastWrite and LastAccess time, the creation, deletion, or renaming of text files in the directory. If a file is changed, created, or deleted, the path to the file prints to the console. When a file is renamed, the old and new paths print to the console. using System; using System.IO; using System.Security.Permissions; public class Watcher { public static void Main() { Run(); } [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] private static void Run() { string[] args = Environment.GetCommandLineArgs(); // If a directory is not specified, exit program. if (args.Length != 2) { // Display the proper way to call the program. Console.WriteLine("Usage: Watcher.exe (directory)"); return; } // Create a new FileSystemWatcher and set its properties. using (FileSystemWatcher watcher = new FileSystemWatcher()) { watcher.Path = args[1]; // Watch for changes in LastAccess and LastWrite times, and // the renaming of files or directories. watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Only watch text files. watcher.Filter = "*.txt"; // Add event handlers. watcher.Changed += OnChanged; watcher.Created += OnChanged; watcher.Deleted += OnChanged; watcher.Renamed += OnRenamed; // Begin watching. watcher.EnableRaisingEvents = true; // Wait for the user to quit the program. Console.WriteLine("Press 'q' to quit the sample."); while (Console.Read() != 'q') ; } } // Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) => // Specify what is done when a file is changed, created, or deleted. Console.WriteLine($"File: {e.FullPath} {e.ChangeType}"); private static void OnRenamed(object source, RenamedEventArgs e) => // Specify what is done when a file is renamed. Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}"); } 參考資料該文章在 2023/12/26 22:20:30 編輯過 |
關鍵字查詢
相關文章
正在查詢... |