FileWatcher.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Tasks;
  9. namespace GeekDesk.Util
  10. {
  11. public class FileWatcher
  12. {
  13. public static Dictionary<FileSystemWatcher, MenuInfo> linkMenuMap = new Dictionary<FileSystemWatcher, MenuInfo>();
  14. /// <summary>
  15. /// 实时文件夹监听
  16. /// </summary>
  17. /// <param name="path"></param>
  18. public static void LinkMenuWatcher(MenuInfo menuInfo)
  19. {
  20. try
  21. {
  22. FileSystemWatcher fileSystemWatcher = new FileSystemWatcher
  23. {
  24. Path = menuInfo.LinkPath,
  25. };
  26. linkMenuMap.Add(fileSystemWatcher, menuInfo);
  27. fileSystemWatcher.EnableRaisingEvents = true;
  28. fileSystemWatcher.Changed += LinkIcon_Changed;
  29. fileSystemWatcher.Deleted += LinkIcon_Deleted;
  30. fileSystemWatcher.Created += LinkIcon_Created;
  31. fileSystemWatcher.Renamed += LinkIcon_Renamed;
  32. } catch (Exception e)
  33. {
  34. LogUtil.WriteErrorLog(e, "添加LinkMenu监听异常");
  35. }
  36. }
  37. private static void LinkIcon_Renamed(object sender, RenamedEventArgs e)
  38. {
  39. IconInfo iconInfo = getIconInfoByPath(sender, e.OldFullPath);
  40. iconInfo.Name = e.Name;
  41. iconInfo.Path = e.FullPath;
  42. }
  43. private static void LinkIcon_Changed(object sender, FileSystemEventArgs e)
  44. {
  45. IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath);
  46. if (iconInfo != null)
  47. {
  48. IconInfo newIconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
  49. iconInfo.BitmapImage = newIconInfo.BitmapImage;
  50. }
  51. }
  52. private static void LinkIcon_Deleted(object sender, FileSystemEventArgs e)
  53. {
  54. IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath);
  55. App.Current.Dispatcher.Invoke(() =>
  56. {
  57. linkMenuMap[sender as FileSystemWatcher].IconList.Remove(iconInfo);
  58. });
  59. }
  60. private static void LinkIcon_Created(object sender, FileSystemEventArgs e)
  61. {
  62. IconInfo iconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
  63. App.Current.Dispatcher.Invoke(() =>
  64. {
  65. linkMenuMap[sender as FileSystemWatcher].IconList.Add(iconInfo);
  66. });
  67. }
  68. private static IconInfo getIconInfoByPath(object sender, string path)
  69. {
  70. MenuInfo menuInfo = linkMenuMap[sender as FileSystemWatcher];
  71. foreach (IconInfo iconInfo in menuInfo.IconList)
  72. {
  73. if (iconInfo.Path.Equals(path))
  74. {
  75. return iconInfo;
  76. }
  77. }
  78. return null;
  79. }
  80. /// <summary>
  81. /// 开启所有菜单监听
  82. /// </summary>
  83. /// <param name="appData"></param>
  84. public static void StartLinkMenuWatcher(AppData appData)
  85. {
  86. foreach (MenuInfo menuInfo in appData.MenuList)
  87. {
  88. if (menuInfo.MenuType == Constant.MenuType.LINK)
  89. {
  90. LinkMenuWatcher(menuInfo);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 移除菜单监听
  96. /// </summary>
  97. /// <param name="menuInfo"></param>
  98. public static void RemoveLinkMenuWatcher(MenuInfo menuInfo)
  99. {
  100. try
  101. {
  102. foreach (FileSystemWatcher watcher in linkMenuMap.Keys)
  103. {
  104. if (linkMenuMap[watcher] == menuInfo)
  105. {
  106. //释放资源
  107. watcher.Changed -= LinkIcon_Changed;
  108. watcher.Created -= LinkIcon_Created;
  109. watcher.Deleted -= LinkIcon_Deleted;
  110. watcher.Renamed -= LinkIcon_Renamed;
  111. watcher.EnableRaisingEvents = false;
  112. watcher.Dispose();
  113. linkMenuMap.Remove(watcher);
  114. }
  115. }
  116. }
  117. catch (Exception e)
  118. {
  119. //nothing
  120. }
  121. }
  122. }
  123. }