FileWatcher.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.6.28 - 2017.6.29
  5. // desc: 文件变更监测
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using Y.Utils.DataUtils.Collections;
  14. namespace Y.Utils.IOUtils.FileManUtils
  15. {
  16. /// <summary>
  17. /// 文件更改通知
  18. /// </summary>
  19. public class FileWatcher
  20. {
  21. /// <summary>
  22. /// 接受文件监控信息的事件委托
  23. /// </summary>
  24. /// <param name="sender"></param>
  25. /// <param name="args"></param>
  26. public delegate void FileWatcherEventHandler(object sender, FileWatcherEventArgs args);
  27. /// <summary>
  28. /// 获取文件监控信息
  29. /// </summary>
  30. public FileWatcherEventHandler eventHandler;
  31. private bool _IsStart = false;
  32. private List<FileSystemWatcher> Watchers = new List<FileSystemWatcher>();
  33. /// <summary>
  34. /// 当前运行状态
  35. /// </summary>
  36. public bool IsStart { get { return _IsStart; } }
  37. /// <summary>
  38. /// 初始化文件监测
  39. /// </summary>
  40. public FileWatcher()
  41. {
  42. DriveInfo[] drives = DriveInfo.GetDrives().Where(x => x.IsReady && (x.DriveType == DriveType.Fixed || x.DriveType == DriveType.Removable)).ToArray();
  43. if (ListTool.HasElements(drives))
  44. {
  45. foreach (var d in drives)
  46. {
  47. //if (d.Name.Contains("C")) continue;
  48. FileSystemWatcher fsw = new FileSystemWatcher(d.Name);
  49. fsw.Created += CreatedEvent;//创建文件或目录
  50. fsw.Changed += ChangedEvent;//更改文件或目录
  51. fsw.Deleted += DeletedEvent;//删除文件或目录
  52. fsw.Renamed += RenamedEvent;//重命名文件或目录
  53. fsw.IncludeSubdirectories = true;
  54. fsw.NotifyFilter = (NotifyFilters)383;
  55. Watchers.Add(fsw);
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// 启动文件监测
  61. /// </summary>
  62. public void Start()
  63. {
  64. _IsStart = true;
  65. if (ListTool.HasElements(Watchers))
  66. {
  67. foreach (var w in Watchers)
  68. {
  69. w.EnableRaisingEvents = true;
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 停止文件监测
  75. /// </summary>
  76. public void Stop()
  77. {
  78. _IsStart = false;
  79. if (ListTool.HasElements(Watchers))
  80. {
  81. foreach (var w in Watchers)
  82. {
  83. w.EnableRaisingEvents = false;
  84. }
  85. }
  86. }
  87. private void DriveMonitor()
  88. {
  89. //监测磁盘的插入拔出
  90. }
  91. private void CreatedEvent(object sender, FileSystemEventArgs e)
  92. {
  93. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  94. }
  95. private void ChangedEvent(object sender, FileSystemEventArgs e)
  96. {
  97. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  98. }
  99. private void DeletedEvent(object sender, FileSystemEventArgs e)
  100. {
  101. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  102. }
  103. private void RenamedEvent(object sender, RenamedEventArgs e)
  104. {
  105. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), e.OldFullPath, e.OldName));
  106. }
  107. }
  108. }