EveryThingUtil.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. string installUtilPath = "C:\\Windows\\Microsoft.NET\\Framework" + (Is64Bit ? "64" : "") + "\\v4.0.30319\\InstallUtil.exe";
  84. Process p = new Process();
  85. p.StartInfo.FileName = everyThingPath;
  86. string arg;
  87. switch(type)
  88. {
  89. default:
  90. arg = "-start-service";
  91. break;
  92. case ServiceType.STOP:
  93. arg = "-stop-service";
  94. break;
  95. case ServiceType.INSTALL:
  96. arg = "-install-service";
  97. break;
  98. case ServiceType.UNINSTALL:
  99. arg = "-uninstall-service";
  100. break;
  101. }
  102. p.StartInfo.Arguments = arg;
  103. p.Start();
  104. }
  105. public static ServiceController GetService(string serviceName)
  106. {
  107. ServiceController[] services = ServiceController.GetServices();
  108. foreach (ServiceController s in services)
  109. {
  110. if (s.ServiceName.ToLower().Equals(serviceName.ToLower()))
  111. {
  112. return s;
  113. }
  114. }
  115. return null;
  116. }
  117. public static void DisableEveryThing(bool uninstall = false)
  118. {
  119. try
  120. {
  121. if (Environment.Is64BitOperatingSystem)
  122. {
  123. EveryThing64.Everything_Exit();
  124. }
  125. else
  126. {
  127. EveryThing32.Everything_Exit();
  128. }
  129. }
  130. catch (Exception e) { }
  131. try
  132. {
  133. if (uninstall)
  134. {
  135. EveryThingService(ServiceType.UNINSTALL);
  136. } else
  137. {
  138. EveryThingService(ServiceType.STOP);
  139. }
  140. }
  141. catch (Exception e) { }
  142. }
  143. public static bool HasNext()
  144. {
  145. return ui < Everything_GetNumResults();
  146. }
  147. public static ObservableCollection<IconInfo> Search(string text)
  148. {
  149. ui = 0;
  150. //EveryThing全盘搜索
  151. Everything_Reset();
  152. EveryThingUtil.Everything_SetSearchW(text);
  153. EveryThingUtil.Everything_SetRequestFlags(
  154. EveryThingConst.EVERYTHING_REQUEST_FILE_NAME
  155. | EveryThingConst.EVERYTHING_REQUEST_PATH
  156. | EveryThingConst.EVERYTHING_REQUEST_DATE_MODIFIED
  157. | EveryThingConst.EVERYTHING_REQUEST_SIZE);
  158. EveryThingUtil.Everything_SetSort(13);
  159. EveryThingUtil.Everything_QueryW(true);
  160. return NextPage();
  161. }
  162. public static ObservableCollection<IconInfo> NextPage()
  163. {
  164. if (ui == 0)
  165. {
  166. pageCount = 40;
  167. } else
  168. {
  169. pageCount = 20;
  170. }
  171. string filePath;
  172. const int bufsize = 260;
  173. StringBuilder buf = new StringBuilder(bufsize);
  174. ObservableCollection<IconInfo> iconBakList = new ObservableCollection<IconInfo>();
  175. for (long count = 0; ui < Everything_GetNumResults() && count < pageCount; count++, ui++)
  176. {
  177. buf.Clear();
  178. EveryThingUtil.Everything_GetResultFullPathName(ui, buf, bufsize);
  179. filePath = buf.ToString();
  180. string tempPath = filePath;
  181. string ext = "";
  182. if (!ImageUtil.IsSystemItem(filePath))
  183. {
  184. ext = System.IO.Path.GetExtension(filePath).ToLower();
  185. }
  186. if (".lnk".Equals(ext))
  187. {
  188. string targetPath = FileUtil.GetTargetPathByLnk(filePath);
  189. if (targetPath != null)
  190. {
  191. filePath = targetPath;
  192. }
  193. }
  194. string name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  195. if (String.IsNullOrEmpty(name))
  196. {
  197. name = tempPath.Substring(tempPath.LastIndexOf("\\"));
  198. }
  199. IconInfo iconInfo = new IconInfo
  200. {
  201. Path_NoWrite = filePath,
  202. LnkPath_NoWrite = tempPath,
  203. BitmapImage_NoWrite = ImageUtil.GetBitmapIconByUnknownPath(filePath),
  204. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath),
  205. Name_NoWrite = name,
  206. };
  207. //缓存信息 异步加载图标
  208. iconBakList.Add(iconInfo);
  209. }
  210. return iconBakList;
  211. }
  212. public static UInt32 Everything_SetSearchW(string lpSearchString)
  213. {
  214. if (Environment.Is64BitOperatingSystem)
  215. {
  216. return EveryThing64.Everything_SetSearchW(lpSearchString);
  217. } else
  218. {
  219. return EveryThing32.Everything_SetSearchW(lpSearchString);
  220. }
  221. }
  222. public static void Everything_SetRequestFlags(UInt32 dwRequestFlags)
  223. {
  224. if (Environment.Is64BitOperatingSystem)
  225. {
  226. EveryThing64.Everything_SetRequestFlags(dwRequestFlags);
  227. }
  228. else
  229. {
  230. EveryThing32.Everything_SetRequestFlags(dwRequestFlags);
  231. }
  232. }
  233. public static void Everything_SetSort(UInt32 dwSortType)
  234. {
  235. if (Environment.Is64BitOperatingSystem)
  236. {
  237. EveryThing64.Everything_SetSort(dwSortType);
  238. }
  239. else
  240. {
  241. EveryThing32.Everything_SetSort(dwSortType);
  242. }
  243. }
  244. public static bool Everything_QueryW(bool bWait)
  245. {
  246. if (Environment.Is64BitOperatingSystem)
  247. {
  248. return EveryThing64.Everything_QueryW(bWait);
  249. }
  250. else
  251. {
  252. return EveryThing32.Everything_QueryW(bWait);
  253. }
  254. }
  255. public static UInt32 Everything_GetNumResults()
  256. {
  257. if (Environment.Is64BitOperatingSystem)
  258. {
  259. return EveryThing64.Everything_GetNumResults();
  260. }
  261. else
  262. {
  263. return EveryThing32.Everything_GetNumResults();
  264. }
  265. }
  266. public static void Everything_GetResultFullPathName(UInt32 nIndex, StringBuilder lpString, UInt32 nMaxCount)
  267. {
  268. if (Environment.Is64BitOperatingSystem)
  269. {
  270. EveryThing64.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
  271. }
  272. else
  273. {
  274. EveryThing32.Everything_GetResultFullPathName(nIndex, lpString, nMaxCount);
  275. }
  276. }
  277. public static void Everything_Reset()
  278. {
  279. if (Environment.Is64BitOperatingSystem)
  280. {
  281. EveryThing64.Everything_Reset();
  282. }
  283. else
  284. {
  285. EveryThing32.Everything_Reset();
  286. }
  287. }
  288. }
  289. }