1
0

FileWatcher.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using GeekDesk.ViewModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace GeekDesk.Util
  11. {
  12. public class FileWatcher
  13. {
  14. public static Dictionary<FileSystemWatcher, MenuInfo> linkMenuMap = new Dictionary<FileSystemWatcher, MenuInfo>();
  15. /// <summary>
  16. /// 实时文件夹监听
  17. /// </summary>
  18. /// <param name="path"></param>
  19. public static void LinkMenuWatcher(MenuInfo menuInfo)
  20. {
  21. try
  22. {
  23. FileSystemWatcher fileSystemWatcher = new FileSystemWatcher
  24. {
  25. Path = menuInfo.LinkPath,
  26. };
  27. linkMenuMap.Add(fileSystemWatcher, menuInfo);
  28. fileSystemWatcher.EnableRaisingEvents = true;
  29. fileSystemWatcher.Changed += LinkIcon_Changed;
  30. fileSystemWatcher.Deleted += LinkIcon_Deleted;
  31. fileSystemWatcher.Created += LinkIcon_Created;
  32. fileSystemWatcher.Renamed += LinkIcon_Renamed;
  33. } catch (Exception e)
  34. {
  35. LogUtil.WriteErrorLog(e, "添加LinkMenu监听异常");
  36. }
  37. }
  38. private static void LinkIcon_Renamed(object sender, RenamedEventArgs e)
  39. {
  40. IconInfo iconInfo = getIconInfoByPath(sender, e.OldFullPath);
  41. iconInfo.Name = e.Name;
  42. iconInfo.Path = e.FullPath;
  43. }
  44. private static void LinkIcon_Changed(object sender, FileSystemEventArgs e)
  45. {
  46. IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath);
  47. if (iconInfo != null)
  48. {
  49. IconInfo newIconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
  50. iconInfo.BitmapImage = newIconInfo.BitmapImage;
  51. }
  52. }
  53. private static void LinkIcon_Deleted(object sender, FileSystemEventArgs e)
  54. {
  55. IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath);
  56. App.Current.Dispatcher.Invoke(() =>
  57. {
  58. linkMenuMap[sender as FileSystemWatcher].IconList.Remove(iconInfo);
  59. });
  60. }
  61. private static void LinkIcon_Created(object sender, FileSystemEventArgs e)
  62. {
  63. IconInfo iconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
  64. App.Current.Dispatcher.Invoke(() =>
  65. {
  66. linkMenuMap[sender as FileSystemWatcher].IconList.Add(iconInfo);
  67. });
  68. }
  69. private static IconInfo getIconInfoByPath(object sender, string path)
  70. {
  71. MenuInfo menuInfo = linkMenuMap[sender as FileSystemWatcher];
  72. foreach (IconInfo iconInfo in menuInfo.IconList)
  73. {
  74. if (iconInfo.Path.Equals(path))
  75. {
  76. return iconInfo;
  77. }
  78. }
  79. return null;
  80. }
  81. /// <summary>
  82. /// 开启所有菜单监听
  83. /// </summary>
  84. /// <param name="appData"></param>
  85. public static void EnableLinkMenuWatcher(AppData appData)
  86. {
  87. foreach (MenuInfo menuInfo in appData.MenuList)
  88. {
  89. if (menuInfo.MenuType == Constant.MenuType.LINK)
  90. {
  91. LinkMenuWatcher(menuInfo);
  92. }
  93. }
  94. RefreshLinkMenuIcon(appData);
  95. }
  96. private static void RefreshLinkMenuIcon(AppData appData)
  97. {
  98. new Thread(() =>
  99. {
  100. try
  101. {
  102. foreach (MenuInfo menuInfo in appData.MenuList)
  103. {
  104. if (menuInfo.MenuType == Constant.MenuType.LINK)
  105. {
  106. DirectoryInfo dirInfo = new DirectoryInfo(menuInfo.LinkPath);
  107. FileSystemInfo[] fileInfos = dirInfo.GetFileSystemInfos();
  108. ObservableCollection<IconInfo> iconList = new ObservableCollection<IconInfo>();
  109. foreach (FileSystemInfo fileInfo in fileInfos)
  110. {
  111. IconInfo iconInfo = CommonCode.GetIconInfoByPath_NoWrite(fileInfo.FullName);
  112. iconList.Add(iconInfo);
  113. }
  114. App.Current.Dispatcher.Invoke(() =>
  115. {
  116. foreach (IconInfo iconInfo in iconList)
  117. {
  118. menuInfo.IconList = null;
  119. menuInfo.IconList = iconList;
  120. }
  121. });
  122. }
  123. }
  124. }
  125. catch (Exception) { }
  126. }).Start();
  127. }
  128. /// <summary>
  129. /// 移除菜单监听
  130. /// </summary>
  131. /// <param name="menuInfo"></param>
  132. public static void RemoveLinkMenuWatcher(MenuInfo menuInfo)
  133. {
  134. try
  135. {
  136. foreach (FileSystemWatcher watcher in linkMenuMap.Keys)
  137. {
  138. if (linkMenuMap[watcher] == menuInfo)
  139. {
  140. //释放资源
  141. watcher.Changed -= LinkIcon_Changed;
  142. watcher.Created -= LinkIcon_Created;
  143. watcher.Deleted -= LinkIcon_Deleted;
  144. watcher.Renamed -= LinkIcon_Renamed;
  145. watcher.EnableRaisingEvents = false;
  146. watcher.Dispose();
  147. linkMenuMap.Remove(watcher);
  148. }
  149. }
  150. }
  151. catch (Exception e)
  152. {
  153. //nothing
  154. }
  155. }
  156. }
  157. }