EveryThingUtil.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Configuration.Install;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.ServiceProcess;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace GeekDesk.Plugins.EveryThing
  18. {
  19. public class EveryThingUtil
  20. {
  21. //每次加载20条
  22. private static long pageCount = 20;
  23. private static UInt32 ui = 0;
  24. public static void EnableEveryThing(int delayTime = 2000)
  25. {
  26. string pluginsPath = Constants.PLUGINS_PATH;
  27. bool Is64Bit = Environment.Is64BitOperatingSystem;
  28. string everyThingPath = pluginsPath + "/EveryThing/" + (Is64Bit ? 64 : 32) + "/EveryThing.exe";
  29. string installUtilPath = "C:\\Windows\\Microsoft.NET\\Framework"+ (Is64Bit ? "64" : "") + "\\v4.0.30319\\InstallUtil.exe";
  30. new Thread(() =>
  31. {
  32. try
  33. {
  34. Thread.Sleep(delayTime);
  35. //判断EveryThing服务是否存在
  36. ServiceController sc = GetService("Everything");
  37. if (sc != null)
  38. {
  39. //判断是否启动
  40. if (sc.Status != ServiceControllerStatus.StartPending
  41. && sc.Status != ServiceControllerStatus.Running)
  42. {
  43. //启动服务
  44. EveryThingService(ServiceType.START);
  45. }
  46. } else
  47. {
  48. //安装服务
  49. EveryThingService(ServiceType.INSTALL);
  50. }
  51. Thread.Sleep(2000);
  52. if (GetService("Everything") != null)
  53. {
  54. //启动程序
  55. Process exeProcess = new Process();
  56. exeProcess.StartInfo.FileName = everyThingPath;
  57. exeProcess.Start();
  58. int waitTime = 5000;
  59. while (true && waitTime > 0)
  60. {
  61. Thread.Sleep(100);
  62. waitTime -= 100;
  63. exeProcess.CloseMainWindow();
  64. }
  65. }
  66. } catch (Exception e)
  67. {
  68. }
  69. }).Start();
  70. }
  71. enum ServiceType
  72. {
  73. START,
  74. STOP,
  75. INSTALL,
  76. UNINSTALL
  77. }
  78. private static void EveryThingService(ServiceType type)
  79. {
  80. string pluginsPath = Constants.PLUGINS_PATH;
  81. bool Is64Bit = Environment.Is64BitOperatingSystem;
  82. string everyThingPath = pluginsPath + "/EveryThing/" + (Is64Bit ? 64 : 32) + "/EveryThing.exe";
  83. Process p = new Process();
  84. p.StartInfo.FileName = everyThingPath;
  85. string arg;
  86. switch(type)
  87. {
  88. default:
  89. p.StartInfo.Verb = "runas";
  90. arg = "-start-service";
  91. break;
  92. case ServiceType.STOP:
  93. arg = "-stop-service";
  94. break;
  95. case ServiceType.INSTALL:
  96. p.StartInfo.Verb = "runas";
  97. arg = "-install-service";
  98. break;
  99. case ServiceType.UNINSTALL:
  100. arg = "-uninstall-service";
  101. break;
  102. }
  103. p.StartInfo.Arguments = arg;
  104. p.Start();
  105. }
  106. public static ServiceController GetService(string serviceName)
  107. {
  108. ServiceController[] services = ServiceController.GetServices();
  109. foreach (ServiceController s in services)
  110. {
  111. if (s.ServiceName.ToLower().Equals(serviceName.ToLower()))
  112. {
  113. return s;
  114. }
  115. }
  116. return null;
  117. }
  118. public static void DisableEveryThing(bool uninstall = false)
  119. {
  120. try
  121. {
  122. if (Environment.Is64BitOperatingSystem)
  123. {
  124. EveryThing64.Everything_Exit();
  125. }
  126. else
  127. {
  128. EveryThing32.Everything_Exit();
  129. }
  130. }
  131. catch (Exception e) { }
  132. try
  133. {
  134. if (GetService("Everything") != null)
  135. {
  136. if (uninstall)
  137. {
  138. EveryThingService(ServiceType.UNINSTALL);
  139. }
  140. else
  141. {
  142. EveryThingService(ServiceType.STOP);
  143. }
  144. }
  145. }
  146. catch (Exception e) { }
  147. }
  148. public static bool HasNext()
  149. {
  150. return ui < Everything_GetNumResults();
  151. }
  152. public static ObservableCollection<IconInfo> Search(string text)
  153. {
  154. ui = 0;
  155. //EveryThing全盘搜索
  156. Everything_Reset();
  157. EveryThingUtil.Everything_SetSearchW(text);
  158. EveryThingUtil.Everything_SetRequestFlags(
  159. EveryThingConst.EVERYTHING_REQUEST_FILE_NAME
  160. | EveryThingConst.EVERYTHING_REQUEST_PATH
  161. | EveryThingConst.EVERYTHING_REQUEST_DATE_MODIFIED
  162. | EveryThingConst.EVERYTHING_REQUEST_SIZE);
  163. EveryThingUtil.Everything_SetSort(
  164. EveryThingConst.EVERYTHING_SORT_TYPE_NAME_DESCENDING
  165. | EveryThingConst.EVERYTHING_SORT_RUN_COUNT_DESCENDING
  166. | EveryThingConst.EVERYTHING_SORT_DATE_MODIFIED_DESCENDING
  167. );
  168. EveryThingUtil.Everything_Query(true);
  169. return NextPage();
  170. }
  171. public static ObservableCollection<IconInfo> NextPage()
  172. {
  173. if (ui == 0)
  174. {
  175. pageCount = 40;
  176. } else
  177. {
  178. pageCount = 20;
  179. }
  180. string filePath;
  181. const int bufsize = 260;
  182. StringBuilder buf = new StringBuilder(bufsize);
  183. ObservableCollection<IconInfo> iconBakList = new ObservableCollection<IconInfo>();
  184. for (long count = 0; ui < Everything_GetNumResults() && count < pageCount; count++, ui++)
  185. {
  186. buf.Clear();
  187. EveryThingUtil.Everything_GetResultFullPathName(ui, buf, bufsize);
  188. filePath = buf.ToString();
  189. string tempPath = filePath;
  190. string ext = "";
  191. if (!ImageUtil.IsSystemItem(filePath))
  192. {
  193. ext = System.IO.Path.GetExtension(filePath).ToLower();
  194. }
  195. //if (".lnk".Equals(ext))
  196. //{
  197. // string targetPath = FileUtil.GetTargetPathByLnk(filePath);
  198. // if (targetPath != null)
  199. // {
  200. // filePath = targetPath;
  201. // }
  202. //}
  203. string name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  204. if (string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(tempPath))
  205. {
  206. name = tempPath.Substring(tempPath.LastIndexOf("\\"));
  207. }
  208. IconInfo iconInfo = new IconInfo
  209. {
  210. Path_NoWrite = filePath,
  211. LnkPath_NoWrite = tempPath,
  212. BitmapImage_NoWrite = ImageUtil.GetBitmapIconByUnknownPath(filePath),
  213. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath),
  214. Name_NoWrite = name,
  215. };
  216. //缓存信息 异步加载图标
  217. iconBakList.Add(iconInfo);
  218. }
  219. return iconBakList;
  220. }
  221. public static UInt32 Everything_SetSearchW(string lpSearchString)
  222. {
  223. if (Environment.Is64BitOperatingSystem)
  224. {
  225. return EveryThing64.Everything_SetSearchW(lpSearchString);
  226. } else
  227. {
  228. return EveryThing32.Everything_SetSearchW(lpSearchString);
  229. }
  230. }
  231. public static void Everything_SetRequestFlags(UInt32 dwRequestFlags)
  232. {
  233. if (Environment.Is64BitOperatingSystem)
  234. {
  235. EveryThing64.Everything_SetRequestFlags(dwRequestFlags);
  236. }
  237. else
  238. {
  239. EveryThing32.Everything_SetRequestFlags(dwRequestFlags);
  240. }
  241. }
  242. public static void Everything_SetSort(UInt32 dwSortType)
  243. {
  244. if (Environment.Is64BitOperatingSystem)
  245. {
  246. EveryThing64.Everything_SetSort(dwSortType);
  247. }
  248. else
  249. {
  250. EveryThing32.Everything_SetSort(dwSortType);
  251. }
  252. }
  253. public static bool Everything_Query(bool bWait)
  254. {
  255. if (Environment.Is64BitOperatingSystem)
  256. {
  257. return EveryThing64.Everything_Query(bWait);
  258. }
  259. else
  260. {
  261. return EveryThing32.Everything_Query(bWait);
  262. }
  263. }
  264. public static UInt32 Everything_GetNumResults()
  265. {
  266. if (Environment.Is64BitOperatingSystem)
  267. {
  268. return EveryThing64.Everything_GetNumResults();
  269. }
  270. else
  271. {
  272. return EveryThing32.Everything_GetNumResults();
  273. }
  274. }
  275. public static void Everything_GetResultFullPathName(UInt32 nIndex, StringBuilder lpString, UInt32 nMaxCount)
  276. {
  277. if (Environment.Is64BitOperatingSystem)
  278. {
  279. EveryThing64.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
  280. }
  281. else
  282. {
  283. EveryThing32.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
  284. }
  285. }
  286. public static void Everything_Reset()
  287. {
  288. if (Environment.Is64BitOperatingSystem)
  289. {
  290. EveryThing64.Everything_Reset();
  291. }
  292. else
  293. {
  294. EveryThing32.Everything_Reset();
  295. }
  296. }
  297. }
  298. }