ProcessUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. }
  159. private static void StartSystemApp(string startArg, IconStartType type)
  160. {
  161. if (type == IconStartType.SHOW_IN_EXPLORE)
  162. {
  163. Growl.WarningGlobal("系统项目不支持打开文件位置操作!");
  164. return;
  165. }
  166. switch (startArg)
  167. {
  168. case "Calculator":
  169. Process.Start("calc.exe");
  170. break;
  171. case "Computer":
  172. Process.Start("explorer.exe");
  173. break;
  174. case "GroupPolicy":
  175. Process.Start("gpedit.msc");
  176. break;
  177. case "Notepad":
  178. Process.Start("notepad");
  179. break;
  180. case "Network":
  181. Process.Start("ncpa.cpl");
  182. break;
  183. case "RecycleBin":
  184. Process.Start("shell:RecycleBinFolder");
  185. break;
  186. case "Registry":
  187. Process.Start("regedit.exe");
  188. break;
  189. case "Mstsc":
  190. if (type == IconStartType.ADMIN_STARTUP)
  191. {
  192. Process.Start("mstsc", "-admin");
  193. }
  194. else
  195. {
  196. Process.Start("mstsc");
  197. }
  198. break;
  199. case "Control":
  200. Process.Start("Control");
  201. break;
  202. case "CMD":
  203. if (type == IconStartType.ADMIN_STARTUP)
  204. {
  205. using (Process process = new Process())
  206. {
  207. process.StartInfo.FileName = "cmd.exe";
  208. process.StartInfo.Verb = "runas";
  209. process.Start();
  210. }
  211. }
  212. else
  213. {
  214. Process.Start("cmd");
  215. }
  216. break;
  217. case "Services":
  218. Process.Start("services.msc");
  219. break;
  220. }
  221. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  222. if (MainWindow.appData.AppConfig.MarginHide)
  223. {
  224. if (!MarginHide.IS_HIDE)
  225. {
  226. MainWindow.HideApp();
  227. }
  228. }
  229. else
  230. {
  231. MainWindow.HideApp();
  232. }
  233. }
  234. public static void ReStartApp()
  235. {
  236. if (MainWindow.appData.AppConfig.MouseMiddleShow || MainWindow.appData.AppConfig.SecondsWindow == true)
  237. {
  238. MouseHookThread.Dispose();
  239. }
  240. Process p = new Process();
  241. p.StartInfo.FileName = Constants.APP_DIR + "GeekDesk.exe";
  242. p.StartInfo.WorkingDirectory = Constants.APP_DIR;
  243. p.Start();
  244. Application.Current.Shutdown();
  245. }
  246. [Flags]
  247. private enum ProcessAccessFlags : uint
  248. {
  249. QueryLimitedInformation = 0x00001000
  250. }
  251. [DllImport("kernel32.dll", SetLastError = true)]
  252. private static extern bool QueryFullProcessImageName(
  253. [In] IntPtr hProcess,
  254. [In] int dwFlags,
  255. [Out] StringBuilder lpExeName,
  256. ref int lpdwSize);
  257. [DllImport("kernel32.dll", SetLastError = true)]
  258. private static extern IntPtr OpenProcess(
  259. ProcessAccessFlags processAccess,
  260. bool bInheritHandle,
  261. int processId);
  262. public static String GetProcessFilename(Process p)
  263. {
  264. int capacity = 2000;
  265. StringBuilder builder = new StringBuilder(capacity);
  266. IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id);
  267. if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
  268. {
  269. return String.Empty;
  270. }
  271. return builder.ToString();
  272. }
  273. }
  274. }