EveryThingUtil.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using GeekDesk.Constant;
  2. using GeekDesk.Plugins.EveryThing.Constant;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace GeekDesk.Plugins.EveryThing
  14. {
  15. public class EveryThingUtil
  16. {
  17. //检查是否是由GeekDesk启动的EveryThing
  18. private static bool IsByGeekDesk = true;
  19. //每次加载20条
  20. private static long pageCount = 20;
  21. private static UInt32 ui = 0;
  22. public static void EnableEveryThing()
  23. {
  24. string pluginsPath = Constants.PLUGINS_PATH;
  25. bool Is64Bit = Environment.Is64BitOperatingSystem;
  26. string everyThingPath = pluginsPath + "/EveryThing/" + (Is64Bit ? 64 : 32) + "/EveryThing.exe";
  27. new Thread(() =>
  28. {
  29. Thread.Sleep(2000);
  30. //判断EveryThing服务是否已启动
  31. bool enabled = false;
  32. Process[] processList = Process.GetProcesses();
  33. foreach (System.Diagnostics.Process process in processList)
  34. {
  35. if (process.ProcessName.ToUpper().Equals("EVERYTHING"))
  36. {
  37. enabled = true;
  38. IsByGeekDesk = false;
  39. break;
  40. }
  41. }
  42. if (!enabled)
  43. {
  44. //启动服务
  45. using (Process p = new Process())
  46. {
  47. p.StartInfo.FileName = everyThingPath;
  48. p.StartInfo.UseShellExecute = true;
  49. p.StartInfo.Verb = "runas";
  50. p.StartInfo.Arguments = " -svc";
  51. p.Start();
  52. }
  53. }
  54. Thread.Sleep(2000);
  55. processList = Process.GetProcesses();
  56. //启动程序
  57. using (Process p = new Process())
  58. {
  59. p.StartInfo.FileName = everyThingPath;
  60. p.Start();
  61. int waitTime = 3000;
  62. while (true && waitTime > 0)
  63. {
  64. Thread.Sleep(100);
  65. waitTime -= 100;
  66. p.CloseMainWindow();
  67. }
  68. }
  69. }).Start();
  70. }
  71. public static void DisableEveryThing()
  72. {
  73. if (IsByGeekDesk)
  74. {
  75. if (Environment.Is64BitOperatingSystem)
  76. {
  77. EveryThing64.Everything_Exit();
  78. } else
  79. {
  80. EveryThing32.Everything_Exit();
  81. }
  82. }
  83. }
  84. public static bool hasNext()
  85. {
  86. return ui < Everything_GetNumResults();
  87. }
  88. public static ObservableCollection<IconInfo> Search(string text)
  89. {
  90. ui = 0;
  91. //EveryThing全盘搜索
  92. Everything_Reset();
  93. EveryThingUtil.Everything_SetSearchW(text);
  94. EveryThingUtil.Everything_SetRequestFlags(
  95. EveryThingConst.EVERYTHING_REQUEST_FILE_NAME
  96. | EveryThingConst.EVERYTHING_REQUEST_PATH
  97. | EveryThingConst.EVERYTHING_REQUEST_DATE_MODIFIED
  98. | EveryThingConst.EVERYTHING_REQUEST_SIZE);
  99. EveryThingUtil.Everything_SetSort(13);
  100. EveryThingUtil.Everything_QueryW(true);
  101. return NextPage();
  102. }
  103. public static ObservableCollection<IconInfo> NextPage()
  104. {
  105. string filePath;
  106. const int bufsize = 260;
  107. StringBuilder buf = new StringBuilder(bufsize);
  108. ObservableCollection<IconInfo> iconBakList = new ObservableCollection<IconInfo>();
  109. for (long count = 0; ui < Everything_GetNumResults() && count < pageCount; count++, ui++)
  110. {
  111. buf.Clear();
  112. EveryThingUtil.Everything_GetResultFullPathName(ui, buf, bufsize);
  113. filePath = buf.ToString();
  114. string tempPath = filePath;
  115. string ext = "";
  116. if (!ImageUtil.IsSystemItem(filePath))
  117. {
  118. ext = System.IO.Path.GetExtension(filePath).ToLower();
  119. }
  120. if (".lnk".Equals(ext))
  121. {
  122. string targetPath = FileUtil.GetTargetPathByLnk(filePath);
  123. if (targetPath != null)
  124. {
  125. filePath = targetPath;
  126. }
  127. }
  128. string name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  129. if (String.IsNullOrEmpty(name))
  130. {
  131. name = tempPath.Substring(tempPath.LastIndexOf("\\"));
  132. }
  133. IconInfo iconInfo = new IconInfo
  134. {
  135. Path_NoWrite = filePath,
  136. LnkPath_NoWrite = tempPath,
  137. BitmapImage_NoWrite = null,
  138. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath),
  139. Name_NoWrite = name,
  140. };
  141. //缓存信息 异步加载图标
  142. iconBakList.Add(iconInfo);
  143. }
  144. return iconBakList;
  145. }
  146. public static UInt32 Everything_SetSearchW(string lpSearchString)
  147. {
  148. if (Environment.Is64BitOperatingSystem)
  149. {
  150. return EveryThing64.Everything_SetSearchW(lpSearchString);
  151. } else
  152. {
  153. return EveryThing32.Everything_SetSearchW(lpSearchString);
  154. }
  155. }
  156. public static void Everything_SetRequestFlags(UInt32 dwRequestFlags)
  157. {
  158. if (Environment.Is64BitOperatingSystem)
  159. {
  160. EveryThing64.Everything_SetRequestFlags(dwRequestFlags);
  161. }
  162. else
  163. {
  164. EveryThing32.Everything_SetRequestFlags(dwRequestFlags);
  165. }
  166. }
  167. public static void Everything_SetSort(UInt32 dwSortType)
  168. {
  169. if (Environment.Is64BitOperatingSystem)
  170. {
  171. EveryThing64.Everything_SetSort(dwSortType);
  172. }
  173. else
  174. {
  175. EveryThing32.Everything_SetSort(dwSortType);
  176. }
  177. }
  178. public static bool Everything_QueryW(bool bWait)
  179. {
  180. if (Environment.Is64BitOperatingSystem)
  181. {
  182. return EveryThing64.Everything_QueryW(bWait);
  183. }
  184. else
  185. {
  186. return EveryThing32.Everything_QueryW(bWait);
  187. }
  188. }
  189. public static UInt32 Everything_GetNumResults()
  190. {
  191. if (Environment.Is64BitOperatingSystem)
  192. {
  193. return EveryThing64.Everything_GetNumResults();
  194. }
  195. else
  196. {
  197. return EveryThing32.Everything_GetNumResults();
  198. }
  199. }
  200. public static void Everything_GetResultFullPathName(UInt32 nIndex, StringBuilder lpString, UInt32 nMaxCount)
  201. {
  202. if (Environment.Is64BitOperatingSystem)
  203. {
  204. EveryThing64.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
  205. }
  206. else
  207. {
  208. EveryThing32.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
  209. }
  210. }
  211. public static void Everything_Reset()
  212. {
  213. if (Environment.Is64BitOperatingSystem)
  214. {
  215. EveryThing64.Everything_Reset();
  216. }
  217. else
  218. {
  219. EveryThing32.Everything_Reset();
  220. }
  221. }
  222. }
  223. }