利用 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/]
微信公眾號
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/240723.html
標籤:C#
下一篇:C#控制臺—生命游戲
