FileWatcher.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. public FileWatcher()
  38. {
  39. //DriveInfo[] drives = DriveInfo.GetDrives().Where(x => x.IsReady && (x.DriveType == DriveType.Fixed || x.DriveType == DriveType.Removable)).ToArray();
  40. //if (ListTool.HasElements(drives))
  41. //{
  42. // foreach (var d in drives)
  43. // {
  44. // //if (d.Name.Contains("C")) continue;
  45. // FileSystemWatcher fsw = new FileSystemWatcher(d.Name);
  46. // fsw.Created += CreatedEvent;//创建文件或目录
  47. // fsw.Changed += ChangedEvent;//更改文件或目录
  48. // fsw.Deleted += DeletedEvent;//删除文件或目录
  49. // fsw.Renamed += RenamedEvent;//重命名文件或目录
  50. // fsw.Error += ErrorEvent;
  51. // fsw.IncludeSubdirectories = true;
  52. // fsw.NotifyFilter = (NotifyFilters)383;
  53. // Watchers.Add(fsw);
  54. // }
  55. //}
  56. }
  57. public FileWatcher(string[] paths)
  58. {
  59. if (ListTool.HasElements(paths))
  60. {
  61. foreach (var p in paths)
  62. {
  63. Add(p);
  64. }
  65. }
  66. }
  67. public void Add(string path, bool start = false)
  68. {
  69. FileSystemWatcher fsw = new FileSystemWatcher(path);
  70. fsw.Created += CreatedEvent;//创建文件或目录
  71. fsw.Changed += ChangedEvent;//更改文件或目录
  72. fsw.Deleted += DeletedEvent;//删除文件或目录
  73. fsw.Renamed += RenamedEvent;//重命名文件或目录
  74. fsw.Error += ErrorEvent;
  75. fsw.IncludeSubdirectories = true;
  76. fsw.NotifyFilter = (NotifyFilters)383;
  77. if (start) fsw.EnableRaisingEvents = start;
  78. Watchers.Add(fsw);
  79. }
  80. public void Remove(string path)
  81. {
  82. for (int i = Watchers.Count - 1; i >= 0; i--)
  83. {
  84. if (Watchers[i].Path == path)
  85. {
  86. Watchers[i].EnableRaisingEvents = false;
  87. Watchers[i].Dispose();
  88. Watchers.RemoveAt(i);
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// 启动文件监测
  94. /// </summary>
  95. public void Start()
  96. {
  97. if (!_IsDisposed)
  98. {
  99. _IsStart = true;
  100. if (ListTool.HasElements(Watchers))
  101. {
  102. foreach (var w in Watchers)
  103. {
  104. w.EnableRaisingEvents = true;
  105. }
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// 停止文件监测
  111. /// </summary>
  112. public void Stop()
  113. {
  114. if (!_IsDisposed)
  115. {
  116. _IsStart = false;
  117. if (ListTool.HasElements(Watchers))
  118. {
  119. foreach (var w in Watchers)
  120. {
  121. w.EnableRaisingEvents = false;
  122. }
  123. }
  124. }
  125. }
  126. private void DriveMonitor()
  127. {
  128. //监测磁盘的插入拔出
  129. }
  130. private void CreatedEvent(object sender, FileSystemEventArgs e)
  131. {
  132. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  133. }
  134. private void ChangedEvent(object sender, FileSystemEventArgs e)
  135. {
  136. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  137. }
  138. private void DeletedEvent(object sender, FileSystemEventArgs e)
  139. {
  140. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  141. }
  142. private void RenamedEvent(object sender, RenamedEventArgs e)
  143. {
  144. eventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), e.OldFullPath, e.OldName));
  145. }
  146. private void ErrorEvent(object sender, ErrorEventArgs e)
  147. {
  148. }
  149. public void Dispose()
  150. {
  151. if (!_IsDisposed)
  152. {
  153. _IsStart = false;
  154. _IsDisposed = true;
  155. if (ListTool.HasElements(Watchers))
  156. {
  157. foreach (var w in Watchers)
  158. {
  159. w.EnableRaisingEvents = false;
  160. w.Dispose();
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }