EveryThingUtil.cs 8.4 KB

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