ProcessUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using GeekDesk.Constant;
  2. using GeekDesk.MyThread;
  3. using GeekDesk.ViewModel;
  4. using HandyControl.Controls;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace GeekDesk.Util
  15. {
  16. public class ProcessUtil
  17. {
  18. public static void StartIconApp(IconInfo icon, IconStartType type, bool useRelativePath = false)
  19. {
  20. App.Current.Dispatcher.Invoke(() =>
  21. {
  22. try
  23. {
  24. using (Process p = new Process())
  25. {
  26. string startArg = icon.StartArg;
  27. if (startArg != null && Constants.SYSTEM_ICONS.ContainsKey(startArg))
  28. {
  29. StartSystemApp(startArg, type);
  30. }
  31. else
  32. {
  33. string path;
  34. if (useRelativePath)
  35. {
  36. string fullPath = Path.Combine(Constants.APP_DIR, icon.RelativePath);
  37. path = Path.GetFullPath(fullPath);
  38. }
  39. else
  40. {
  41. path = icon.Path;
  42. }
  43. p.StartInfo.FileName = path;
  44. if (!StringUtil.IsEmpty(startArg))
  45. {
  46. p.StartInfo.Arguments = startArg;
  47. }
  48. if (icon.IconType == IconType.OTHER)
  49. {
  50. if (!File.Exists(path) && !Directory.Exists(path))
  51. {
  52. //如果没有使用相对路径 那么使用相对路径启动一次
  53. if (!useRelativePath)
  54. {
  55. StartIconApp(icon, type, true);
  56. return;
  57. }
  58. else
  59. {
  60. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(文件路径不存在或已删除)!");
  61. return;
  62. }
  63. }
  64. p.StartInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("\\"));
  65. switch (type)
  66. {
  67. case IconStartType.ADMIN_STARTUP:
  68. //p.StartInfo.Arguments = "1";//启动参数
  69. p.StartInfo.Verb = "runas";
  70. //p.StartInfo.CreateNoWindow = false; //设置显示窗口
  71. p.StartInfo.UseShellExecute = true;//不使用操作系统外壳程序启动进程
  72. //p.StartInfo.ErrorDialog = false;
  73. if (MainWindow.appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL)
  74. {
  75. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  76. if (MainWindow.appData.AppConfig.MarginHide)
  77. {
  78. if (!MarginHide.IsMargin())
  79. {
  80. MainWindow.HideApp();
  81. }
  82. }
  83. else
  84. {
  85. MainWindow.HideApp();
  86. }
  87. }
  88. break;// c#好像不能case穿透
  89. case IconStartType.DEFAULT_STARTUP:
  90. if (MainWindow.appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL)
  91. {
  92. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  93. if (MainWindow.appData.AppConfig.MarginHide)
  94. {
  95. if (!MarginHide.IsMargin())
  96. {
  97. MainWindow.HideApp();
  98. }
  99. }
  100. else
  101. {
  102. MainWindow.HideApp();
  103. }
  104. }
  105. break;
  106. case IconStartType.SHOW_IN_EXPLORE:
  107. p.StartInfo.FileName = "Explorer.exe";
  108. p.StartInfo.Arguments = "/e,/select," + icon.Path;
  109. break;
  110. }
  111. }
  112. else
  113. {
  114. if (MainWindow.appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL)
  115. {
  116. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  117. if (MainWindow.appData.AppConfig.MarginHide)
  118. {
  119. if (!MarginHide.IS_HIDE)
  120. {
  121. MainWindow.HideApp();
  122. }
  123. }
  124. else
  125. {
  126. MainWindow.HideApp();
  127. }
  128. }
  129. }
  130. p.Start();
  131. if (useRelativePath)
  132. {
  133. //如果使用相对路径启动成功 那么重新设置程序绝对路径
  134. icon.Path = path;
  135. }
  136. }
  137. }
  138. icon.Count++;
  139. //隐藏搜索框
  140. //if (RunTimeStatus.SEARCH_BOX_SHOW)
  141. //{
  142. // MainWindow.mainWindow.HidedSearchBox();
  143. //}
  144. }
  145. catch (Exception e)
  146. {
  147. if (!useRelativePath)
  148. {
  149. StartIconApp(icon, type, true);
  150. }
  151. else
  152. {
  153. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(可能为不支持的启动方式)!");
  154. LogUtil.WriteErrorLog(e, "程序启动失败:path=" + icon.Path + ",type=" + type);
  155. }
  156. }
  157. //启动后根据是否开启了使用次数排序判断是否执行一次排序
  158. CommonCode.SortIconList(MainWindow.appData.AppConfig.IconSortType == (SortType.COUNT_LOW|SortType.COUNT_UP) ? true : false);
  159. });
  160. }
  161. private static void StartSystemApp(string startArg, IconStartType type)
  162. {
  163. if (type == IconStartType.SHOW_IN_EXPLORE)
  164. {
  165. Growl.WarningGlobal("系统项目不支持打开文件位置操作!");
  166. return;
  167. }
  168. switch (startArg)
  169. {
  170. case "Calculator":
  171. Process.Start("calc.exe");
  172. break;
  173. case "Computer":
  174. Process.Start("explorer.exe");
  175. break;
  176. case "GroupPolicy":
  177. Process.Start("gpedit.msc");
  178. break;
  179. case "Notepad":
  180. Process.Start("notepad");
  181. break;
  182. case "Network":
  183. Process.Start("ncpa.cpl");
  184. break;
  185. case "RecycleBin":
  186. Process.Start("shell:RecycleBinFolder");
  187. break;
  188. case "Registry":
  189. Process.Start("regedit.exe");
  190. break;
  191. case "Mstsc":
  192. if (type == IconStartType.ADMIN_STARTUP)
  193. {
  194. Process.Start("mstsc", "-admin");
  195. }
  196. else
  197. {
  198. Process.Start("mstsc");
  199. }
  200. break;
  201. case "Control":
  202. Process.Start("Control");
  203. break;
  204. case "CMD":
  205. if (type == IconStartType.ADMIN_STARTUP)
  206. {
  207. using (Process process = new Process())
  208. {
  209. process.StartInfo.FileName = "cmd.exe";
  210. process.StartInfo.Verb = "runas";
  211. process.Start();
  212. }
  213. }
  214. else
  215. {
  216. Process.Start("cmd");
  217. }
  218. break;
  219. case "Services":
  220. Process.Start("services.msc");
  221. break;
  222. }
  223. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  224. if (MainWindow.appData.AppConfig.MarginHide)
  225. {
  226. if (!MarginHide.IS_HIDE)
  227. {
  228. MainWindow.HideApp();
  229. }
  230. }
  231. else
  232. {
  233. MainWindow.HideApp();
  234. }
  235. }
  236. public static void ReStartApp()
  237. {
  238. if (MainWindow.appData.AppConfig.MouseMiddleShow || MainWindow.appData.AppConfig.SecondsWindow == true)
  239. {
  240. MouseHookThread.Dispose();
  241. }
  242. Process p = new Process();
  243. p.StartInfo.FileName = Constants.APP_DIR + "GeekDesk.exe";
  244. p.StartInfo.WorkingDirectory = Constants.APP_DIR;
  245. p.Start();
  246. Application.Current.Shutdown();
  247. }
  248. [Flags]
  249. private enum ProcessAccessFlags : uint
  250. {
  251. QueryLimitedInformation = 0x00001000
  252. }
  253. [DllImport("kernel32.dll", SetLastError = true)]
  254. private static extern bool QueryFullProcessImageName(
  255. [In] IntPtr hProcess,
  256. [In] int dwFlags,
  257. [Out] StringBuilder lpExeName,
  258. ref int lpdwSize);
  259. [DllImport("kernel32.dll", SetLastError = true)]
  260. private static extern IntPtr OpenProcess(
  261. ProcessAccessFlags processAccess,
  262. bool bInheritHandle,
  263. int processId);
  264. public static String GetProcessFilename(Process p)
  265. {
  266. int capacity = 2000;
  267. StringBuilder builder = new StringBuilder(capacity);
  268. IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id);
  269. if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
  270. {
  271. return String.Empty;
  272. }
  273. return builder.ToString();
  274. }
  275. }
  276. }