FileWatcher.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 : IDisposable
  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, _IsDisposed = 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.Error += ErrorEvent;
  54. fsw.IncludeSubdirectories = true;
  55. fsw.NotifyFilter = (NotifyFilters)383;
  56. Watchers.Add(fsw);
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// 启动文件监测
  62. /// </summary>
  63. public void Start()
  64. {
  65. if (!_IsDisposed)
  66. {
  67. _IsStart = true;
  68. if (ListTool.HasElements(Watchers))
  69. {
  70. foreach (var w in Watchers)
  71. {
  72. w.EnableRaisingEvents = true;
  73. }
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// 停止文件监测
  79. /// </summary>
  80. public void Stop()
  81. {
  82. if (!_IsDisposed)
  83. {
  84. _IsStart = false;
  85. if (ListTool.HasElements(Watchers))
  86. {
  87. foreach (var w in Watchers)
  88. {
  89. w.EnableRaisingEvents = false;
  90. }
  91. }
  92. }
  93. }
  94. private void DriveMonitor()
  95. {
  96. //监测磁盘的插入拔出
  97. }
  98. private void CreatedEvent(object sender, FileSystemEventArgs e)
  99. {
  100. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  101. }
  102. private void ChangedEvent(object sender, FileSystemEventArgs e)
  103. {
  104. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  105. }
  106. private void DeletedEvent(object sender, FileSystemEventArgs e)
  107. {
  108. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  109. }
  110. private void RenamedEvent(object sender, RenamedEventArgs e)
  111. {
  112. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), e.OldFullPath, e.OldName));
  113. }
  114. private void ErrorEvent(object sender, ErrorEventArgs e)
  115. {
  116. }
  117. public void Dispose()
  118. {
  119. if (!_IsDisposed)
  120. {
  121. _IsStart = false;
  122. _IsDisposed = true;
  123. if (ListTool.HasElements(Watchers))
  124. {
  125. foreach (var w in Watchers)
  126. {
  127. w.EnableRaisingEvents = false;
  128. w.Dispose();
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }