利用 C# 中的 FileSystemWatcher 制作一個文件夾監控小工具
前一段看到微信公眾號 “碼農讀書” 上發了一篇文章《如何使用 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/]