C#中FileSystemWatcher的使用教程
開局一張圖,在 System.IO 下的 FileSystemWatcher 常用于監(jiān)視文件系統(tǒng)的變更,當文件系統(tǒng)中的文件或者文件夾被修改會自動觸發(fā)相應的回調(diào)事件。
為了能夠了解 FileSystemWatcher 是怎么運作的,你可以指定一個被監(jiān)視的文件夾,當被監(jiān)視的文件夾修改后,大概會觸發(fā)如下的一些事件。
- Changed: 當文件或者文件夾已經(jīng)完成修改時觸發(fā)此事件
- Created:當文件或者文件夾已經(jīng)成功創(chuàng)建觸發(fā)此事件
- Deleted:當文件或者文件夾已經(jīng)成功刪除觸發(fā)此事件
- Error:當變更的過程中發(fā)生錯誤觸發(fā)此事件。
- Renamed:當文件或者文件夾已經(jīng)成功被重命名時觸發(fā)此事件
創(chuàng)建一個簡單的 file 監(jiān)視
接下來我們在 Visual Studio 中創(chuàng)建一個 Console 程序,用來了解如何進行文件監(jiān)視,不過建議把 Console 部署成 Windows Service,這樣方便在系統(tǒng)中常駐監(jiān)控,一旦被監(jiān)視的路徑發(fā)生變更就會自動發(fā)出通知事件。
該說的都說了,接下來一起研究代碼吧。
static void Main(string[] args) { string path = @"D:\IDG"; MonitorDirectory(path); Console.ReadKey(); }
下面的代碼片段展示了 MonitorDirectory 方法的內(nèi)部邏輯,這個方法可用于監(jiān)視指定的文件夾并且當文件夾變更時觸發(fā)事件,可以看到文件夾路徑是通過參數(shù)傳進去的。
private static void MonitorDirectory(string path) { FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); fileSystemWatcher.Path = path; fileSystemWatcher.Created += FileSystemWatcher_Created; fileSystemWatcher.Renamed += FileSystemWatcher_Renamed; fileSystemWatcher.Deleted += FileSystemWatcher_Deleted; fileSystemWatcher.EnableRaisingEvents = true; }
可以著重了解一下上面的 event 是如何被灌入的,而且我還寫了一句 fileSystemWatcher.EnableRaisingEvents = true, 這是什么意思呢?表示當前的路徑正式開始被監(jiān)控,一旦監(jiān)控的路徑出現(xiàn)變更,F(xiàn)ileSystemWatcher 中的指定事件將會被觸發(fā)。
掃一下上面定義的各個 event 事件,分別都定義了該事件的處理函數(shù),比如說:FileSystemWatcher_Created,F(xiàn)ileSystemWatcher_Renamed,FileSystemWatcher_Deleted ,顯而易見觸發(fā)某一個事件就會觸發(fā)它的事件處理函數(shù),對吧,具體代碼如下:
private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e) { Console.WriteLine("File created: {0}", e.Name); } private static void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e) { Console.WriteLine("File renamed: {0}", e.Name); } private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e) { Console.WriteLine("File deleted: {0}", e.Name); }
下面是完整的可供參考的代碼清單。
using System; using System.IO; namespace IDGFileSystemWatcher { class Program { static void Main(string[] args) { string path = @"D:\IDG"; MonitorDirectory(path); Console.ReadKey(); } private static void MonitorDirectory(string path) { FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); fileSystemWatcher.Path = path; fileSystemWatcher.Created += FileSystemWatcher_Created; fileSystemWatcher.Renamed += FileSystemWatcher_Renamed; fileSystemWatcher.Deleted += FileSystemWatcher_Deleted; fileSystemWatcher.EnableRaisingEvents = true; } private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e) { Console.WriteLine("File created: {0}", e.Name); } private static void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e) { Console.WriteLine("File renamed: {0}", e.Name); } private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e) { Console.WriteLine("File deleted: {0}", e.Name); } } }
假設 IDG 文件夾是在 E 盤內(nèi),接下來把 Console 運行起來,然后在 IDG 文件夾內(nèi)創(chuàng)建一個新文件,不出意外的話,你會觀察到這個新建的文件名將會出現(xiàn)在 控制臺 上,說明 FileSystemWatcher_Created 被成功觸發(fā),參考下圖:
譯文鏈接:https://www.infoworld.com/article/3185447/how-to-work-with-filesystemwatcher-in-c.html
到此這篇關于C#中FileSystemWatcher使用教程的文章就介紹到這了,更多相關C# FileSystemWatcher使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Unity讀取Excel文件轉(zhuǎn)換XML格式文件
這篇文章主要為大家詳細介紹了Unity讀取Excel文件轉(zhuǎn)換XML格式文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06C#中DataTable的創(chuàng)建與遍歷實現(xiàn)
這篇文章主要介紹了C#中DataTable的創(chuàng)建與遍歷實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02