FileWatcher.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Azylee.Core.DataUtils.CollectionUtils;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace Azylee.Core.IOUtils.FileManUtils
  11. {
  12. /// <summary>
  13. /// 文件更改通知
  14. /// </summary>
  15. public class FileWatcher
  16. {
  17. /// <summary>
  18. /// 接受文件监控信息的事件委托
  19. /// </summary>
  20. /// <param name="sender"></param>
  21. /// <param name="args"></param>
  22. public delegate void FileWatcherEventHandler(object sender, FileWatcherEventArgs args);
  23. /// <summary>
  24. /// 获取文件监控信息
  25. /// </summary>
  26. public FileWatcherEventHandler EventHandler;
  27. private int Interval = 10 * 1000;
  28. private bool _IsWatching = false;
  29. private ConcurrentDictionary<string, FileSystemWatcher> Watchers = new ConcurrentDictionary<string, FileSystemWatcher>();
  30. /// <summary>
  31. /// 文件更改监控已启动
  32. /// </summary>
  33. public bool IsWatching { get { return _IsWatching; } }
  34. /// <summary>
  35. /// 创建文件监控类
  36. /// </summary>
  37. /// <param name="paths"></param>
  38. public FileWatcher(string[] paths)
  39. {
  40. if (ListTool.HasElements(paths))
  41. {
  42. foreach (var p in paths)
  43. {
  44. if (Directory.Exists(p) && !Watchers.ContainsKey(p))
  45. Watchers.TryAdd(p, null);
  46. }
  47. }
  48. }
  49. public bool AddPath(string path)
  50. {
  51. if (Directory.Exists(path) && !Watchers.ContainsKey(path))
  52. return Watchers.TryAdd(path, null);
  53. return false;
  54. }
  55. public bool DelPath(string path)
  56. {
  57. if (Watchers.ContainsKey(path))
  58. {
  59. FileSystemWatcher temp;
  60. if (Watchers.TryRemove(path, out temp) && temp != null)
  61. {
  62. temp.EnableRaisingEvents = false;
  63. temp.Dispose();
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. /// <summary>
  70. /// 启动文件监测
  71. /// </summary>
  72. public void Start()
  73. {
  74. if (!_IsWatching)
  75. {
  76. _IsWatching = true;
  77. Task.Factory.StartNew(() =>
  78. {
  79. while (_IsWatching)
  80. {
  81. foreach (var item in Watchers)
  82. {
  83. if (item.Value == null)
  84. {
  85. if (Directory.Exists(item.Key))
  86. {
  87. Watchers.TryUpdate(item.Key, CreateWatcher(item.Key), null);
  88. }
  89. }
  90. else
  91. {
  92. if (!Directory.Exists(item.Key))
  93. {
  94. item.Value.EnableRaisingEvents = false;
  95. item.Value.Dispose();
  96. Watchers.TryUpdate(item.Key, null, item.Value);
  97. }
  98. }
  99. }
  100. Thread.Sleep(Interval);
  101. }
  102. _IsWatching = false;
  103. });
  104. }
  105. }
  106. /// <summary>
  107. /// 停止文件监测
  108. /// </summary>
  109. public void Stop()
  110. {
  111. _IsWatching = false;
  112. foreach (var item in Watchers.Keys)
  113. {
  114. if (Watchers.ContainsKey(item))
  115. {
  116. if (Watchers[item] != null) Watchers[item].EnableRaisingEvents = false;
  117. FileSystemWatcher temp;
  118. if (Watchers.TryRemove(item, out temp) && temp != null)
  119. {
  120. temp.EnableRaisingEvents = false;
  121. temp.Dispose();
  122. }
  123. }
  124. }
  125. }
  126. private FileSystemWatcher CreateWatcher(string path)
  127. {
  128. FileSystemWatcher fsw = new FileSystemWatcher(path);
  129. fsw.Created += CreatedEvent;//创建文件或目录
  130. fsw.Changed += ChangedEvent;//更改文件或目录
  131. fsw.Deleted += DeletedEvent;//删除文件或目录
  132. fsw.Renamed += RenamedEvent;//重命名文件或目录
  133. fsw.Error += ErrorEvent;
  134. fsw.IncludeSubdirectories = true;
  135. fsw.NotifyFilter = (NotifyFilters)383;
  136. fsw.EnableRaisingEvents = true;
  137. return fsw;
  138. }
  139. private void CreatedEvent(object sender, FileSystemEventArgs e)
  140. {
  141. EventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  142. }
  143. private void ChangedEvent(object sender, FileSystemEventArgs e)
  144. {
  145. EventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  146. }
  147. private void DeletedEvent(object sender, FileSystemEventArgs e)
  148. {
  149. EventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), null, null));
  150. }
  151. private void RenamedEvent(object sender, RenamedEventArgs e)
  152. {
  153. EventHandler?.Invoke(sender, new FileWatcherEventArgs(e.ChangeType, e.FullPath, Path.GetFileName(e.FullPath), e.OldFullPath, e.OldName));
  154. }
  155. private void ErrorEvent(object sender, ErrorEventArgs e)
  156. { }
  157. }
  158. }