1. 程式人生 > 實用技巧 >利用 C# 中的 FileSystemWatcher 製作一個資料夾監控小工具

利用 C# 中的 FileSystemWatcher 製作一個資料夾監控小工具

利用 C# 中的 FileSystemWatcher 製作一個資料夾監控小工具

獨立觀察員 2020 年 12 月 26 日

前一段看到微信公眾號 “碼農讀書” 上發了一篇文章《如何使用 C# 中的 FileSystemWatcher》(翻譯自:https://www.infoworld.com/article/3185447/how-to-work-with-filesystemwatcher-in-c.html),其中簡述了使用FileSystemWatcher進行檔案系統變更監測的方法,本人受此啟發,決定製作一個資料夾變動監控的小工具,當作練手和自用。目前該工具已製作完成,故發文分享給大家。

功能比較簡單,執行程式後,點選 “選擇資料夾” 按鈕選擇想要

監控的資料夾,然後點選 “開始監控檔案變動” 即可。可以檢測資料夾 / 檔案 的建立、刪除、修改、重新命名,然後在資訊窗中輸出相關資訊。如果取消勾選 “是否顯示完全路徑”,則輸出的資訊中將不包含選擇的 “資料夾路徑” 部分,也就是顯示的是相對路徑。如果取消勾選 “是否監控子資料夾”,則程式將不監控子資料夾內的變動情況。

儲存配置按鈕可進行儲存如下資訊,下次開啟程式會恢復儲存的狀態:

關鍵程式碼如下(文末會給出程式碼倉庫地址):

#region 資料夾監控

private FileSystemWatcher _FileSystemWatcher = new FileSystemWatcher();

//參考:https://www.infoworld.com/article/3185447/how-to-work-with-filesystemwatcher-in-c.html /// <summary> /// 開始監控目錄 /// </summary> /// <param name="path">目錄路徑</param> /// <param name="isIncludeSubDir">是否包括子目錄</param> private async void MonitorDirectory(string path, bool isIncludeSubDir = true
) { _FileSystemWatcher.EnableRaisingEvents = false; _FileSystemWatcher = new FileSystemWatcher(); _FileSystemWatcher.Path = path; _FileSystemWatcher.IncludeSubdirectories = isIncludeSubDir; _FileSystemWatcher.Created += FileSystemWatcher_Created; _FileSystemWatcher.Renamed += FileSystemWatcher_Renamed; _FileSystemWatcher.Deleted += FileSystemWatcher_Deleted; _FileSystemWatcher.Changed += FileSystemWatcher_Changed; //開始監控 _FileSystemWatcher.EnableRaisingEvents = true; await ConfirmBoxHelper.ShowMessage(DialogVm, $"已開啟監控:[{Configs.FolderPath}]"); } private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e) { Console.WriteLine($"【{GetPathType(e.FullPath)}更改】{GetPath(e)}"); } private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e) { Console.WriteLine($"【{GetPathType(e.FullPath)}建立】{GetPath(e)}"); } private void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e) { Console.WriteLine($"【{GetPathType(e.FullPath)}重新命名】{GetOldPath((RenamedEventArgs)e)} --> {GetPath(e)}"); } private void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e) { Console.WriteLine($"【{GetPathType(e.FullPath)}刪除】{GetPath(e)}"); } /// <summary> /// 獲取變動的路徑的顯示字串 /// </summary> private string GetPath(FileSystemEventArgs e) { if (Configs.IsShowFullPath) { return e.FullPath; } return e.Name; } /// <summary> /// 獲取原先路徑的顯示字串 /// </summary> private string GetOldPath(RenamedEventArgs e) { if (Configs.IsShowFullPath) { return e.OldFullPath; } return e.OldName; } #endregion #region 判斷是檔案還是資料夾 /// <summary> /// 獲取路徑型別(判斷是檔案還是資料夾) /// </summary> /// <param name="path">路徑</param> /// <returns>PathTypeEnum</returns> public static PathTypeEnum GetPathType(string path) { if (File.Exists(path)) { return PathTypeEnum.檔案; } else if (Directory.Exists(path)) { return PathTypeEnum.資料夾; } else { return PathTypeEnum.不存在; } } /// <summary> /// 路徑型別列舉 /// </summary> public enum PathTypeEnum { 檔案, 資料夾, 不存在 } #endregion

值得注意的就是,FileSystemWatcher開啟和關閉監控是通過 EnableRaisingEvents 這個 bool 屬性進行控制的。然後就是主要的四個事件,增、刪、改、重新命名,分別指定好回撥方法:

_FileSystemWatcher.Created += FileSystemWatcher_Created;
_FileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
_FileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
_FileSystemWatcher.Changed += FileSystemWatcher_Changed;​

還有一點就是,其它事件的引數都是FileSystemEventArgs 型別,而重新命名事件的獨有引數是RenamedEventArgs 型別,這個是前者的子類,多了舊的檔名和路徑等資訊。

程式和程式碼都展示完了,又到了和大家說再見的時刻了,在此附上程式碼地址和另一篇參考文章吧:

程式碼地址:https://gitee.com/dlgcy/DLGCY.FilesWatcher

發行版地址:https://gitee.com/dlgcy/DLGCY.FilesWatcher/releases

又一參考:《FileSystemWatcher 用法詳解》(https://blog.csdn.net/hwt0101/article/details/8469285)(裡面也有個監控軟體,不過我沒下載,大家可以試試)

待更新:目前資訊視窗資訊多的話會觸發 “滅霸模式”,後面考慮加個開關。

好了,就到這裡吧,謝謝閱讀。

原創文章,轉載請註明:轉載自獨立觀察員・部落格

本文連結地址:利用 C# 中的 FileSystemWatcher 製作一個資料夾監控小工具 [http://dlgcy.com/files-watcher/]

微信公眾號