经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
利用 C# 中的 FileSystemWatcher 制作一个文件夹监控小工具
来源:cnblogs  作者:独立观察员  时间:2020/12/28 10:00:46  对本文有异议

利用 C# 中的 FileSystemWatcher 制作一个文件夹监控小工具

独立观察员 2020 年 12 月 26 日

 

前一段看到微信公众号 “码农读书” 上发了一篇文章《如何使用 C# 中的 FileSystemWatcher》(翻译自:https://www.infoworld.com/article/3185447/how-to-work-with-filesystemwatcher-in-c.html ),其中简述了使用 FileSystemWatcher 进行文件系统变更监测的方法,本人受此启发,决定制作一个文件夹变动监控的小工具,当作练手和自用。目前该工具已制作完成,故发文分享给大家。

利用 C# 中的 FileSystemWatcher 制作一个文件夹监控小工具插图

 

功能比较简单,运行程序后,点击 “选择文件夹” 按钮选择想要监控的文件夹,然后点击 “开始监控文件变动” 即可。可以检测 文件夹 / 文件 的创建、删除、修改、重命名,然后在信息窗中输出相关信息。如果取消勾选 “是否显示完全路径”,则输出的信息中将不包含选择的 “文件夹路径” 部分,也就是显示的是相对路径。如果取消勾选 “是否监控子文件夹”,则程序将不监控子文件夹内的变动情况。

 

保存配置按钮可进行保存如下信息,下次打开程序会恢复保存的状态:

利用 C# 中的 FileSystemWatcher 制作一个文件夹监控小工具插图1

 

关键代码如下(文末会给出代码仓库地址):

  1. #region 文件夹监控
  2.  
  3. private FileSystemWatcher _FileSystemWatcher = new FileSystemWatcher();
  4. //参考:https://www.infoworld.com/article/3185447/how-to-work-with-filesystemwatcher-in-c.html
  5.  
  6. /// <summary>
  7. /// 开始监控目录
  8. /// </summary>
  9. /// <param name="path">目录路径</param>
  10. /// <param name="isIncludeSubDir">是否包括子目录</param>
  11. private async void MonitorDirectory(string path, bool isIncludeSubDir = true)
  12. {
  13. _FileSystemWatcher.EnableRaisingEvents = false;
  14. _FileSystemWatcher = new FileSystemWatcher();
  15. _FileSystemWatcher.Path = path;
  16. _FileSystemWatcher.IncludeSubdirectories = isIncludeSubDir;
  17. _FileSystemWatcher.Created += FileSystemWatcher_Created;
  18. _FileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
  19. _FileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
  20. _FileSystemWatcher.Changed += FileSystemWatcher_Changed;
  21. //开始监控
  22. _FileSystemWatcher.EnableRaisingEvents = true;
  23. await ConfirmBoxHelper.ShowMessage(DialogVm, $"已开启监控:[{Configs.FolderPath}]");
  24. }
  25. private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
  26. {
  27. Console.WriteLine($"【{GetPathType(e.FullPath)}更改】{GetPath(e)}");
  28. }
  29. private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
  30. {
  31. Console.WriteLine($"【{GetPathType(e.FullPath)}创建】{GetPath(e)}");
  32. }
  33. private void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e)
  34. {
  35. Console.WriteLine($"【{GetPathType(e.FullPath)}重命名】{GetOldPath((RenamedEventArgs)e)} --> {GetPath(e)}");
  36. }
  37. private void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
  38. {
  39. Console.WriteLine($"【{GetPathType(e.FullPath)}删除】{GetPath(e)}");
  40. }
  41. /// <summary>
  42. /// 获取变动的路径的显示字符串
  43. /// </summary>
  44. private string GetPath(FileSystemEventArgs e)
  45. {
  46. if (Configs.IsShowFullPath)
  47. {
  48. return e.FullPath;
  49. }
  50. return e.Name;
  51. }
  52. /// <summary>
  53. /// 获取原先路径的显示字符串
  54. /// </summary>
  55. private string GetOldPath(RenamedEventArgs e)
  56. {
  57. if (Configs.IsShowFullPath)
  58. {
  59. return e.OldFullPath;
  60. }
  61. return e.OldName;
  62. }
  63. #endregion
  64.  
  65. #region 判断是文件还是文件夹
  66.  
  67. /// <summary>
  68. /// 获取路径类型(判断是文件还是文件夹)
  69. /// </summary>
  70. /// <param name="path">路径</param>
  71. /// <returns>PathTypeEnum</returns>
  72. public static PathTypeEnum GetPathType(string path)
  73. {
  74. if (File.Exists(path))
  75. {
  76. return PathTypeEnum.文件;
  77. }
  78. else if (Directory.Exists(path))
  79. {
  80. return PathTypeEnum.文件夹;
  81. }
  82. else
  83. {
  84. return PathTypeEnum.不存在;
  85. }
  86. }
  87. /// <summary>
  88. /// 路径类型枚举
  89. /// </summary>
  90. public enum PathTypeEnum
  91. {
  92. 文件, 文件夹, 不存在
  93. }
  94. #endregion

 

值得注意的就是,FileSystemWatcher 开启和关闭监控是通过 EnableRaisingEvents 这个 bool 属性进行控制的。然后就是主要的四个事件,增、删、改、重命名,分别指定好回调方法:

  1. _FileSystemWatcher.Created += FileSystemWatcher_Created;
  2. _FileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
  3. _FileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
  4. _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/]

微信公众号

 

原文链接:http://www.cnblogs.com/weiliuhong/p/files-watcher.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号