ProcessTool.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.10.12 - 2018.5.28
  5. // desc: 进程工具
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using Azylee.Core.DataUtils.CollectionUtils;
  9. using Azylee.Core.ThreadUtils.SleepUtils;
  10. using System;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Threading.Tasks;
  14. namespace Azylee.Core.ProcessUtils
  15. {
  16. public static class ProcessTool
  17. {
  18. /// <summary>
  19. /// 判断进程是否存在
  20. /// </summary>
  21. /// <param name="name">进程名</param>
  22. /// <returns></returns>
  23. public static bool IsExists(string name)
  24. {
  25. try
  26. {
  27. var p = Process.GetProcessesByName(name);
  28. if (ListTool.HasElements(p)) return true;
  29. }
  30. catch { }
  31. return false;
  32. }
  33. /// <summary>
  34. /// 启动程序
  35. /// </summary>
  36. /// <param name="file">文件路径</param>
  37. /// <param name="args">启动参数</param>
  38. /// <returns></returns>
  39. public static bool Start(string file, string args = "")
  40. {
  41. if (File.Exists(file))
  42. {
  43. try
  44. {
  45. Process.Start(file, args);
  46. return true;
  47. }
  48. catch { }
  49. }
  50. return false;
  51. }
  52. /// <summary>
  53. /// 启动进程(定制启动配置)
  54. /// </summary>
  55. /// <param name="args"></param>
  56. public static bool StartCustom(string file, string args = "")
  57. {
  58. try
  59. {
  60. if (File.Exists(file))
  61. {
  62. Process p = new Process();
  63. p.StartInfo.FileName = file;
  64. p.StartInfo.Arguments = args;
  65. p.StartInfo.UseShellExecute = true;
  66. p.Start();
  67. p.WaitForInputIdle(3000);
  68. return true;
  69. }
  70. }
  71. catch (Exception ex) { }
  72. return false;
  73. }
  74. /// <summary>
  75. /// 停止进程
  76. /// </summary>
  77. /// <param name="name">进程名</param>
  78. public static void Kill(string name)
  79. {
  80. try
  81. {
  82. Process[] processes = Process.GetProcessesByName(name);
  83. foreach (Process p in processes)
  84. {
  85. p.Kill();
  86. p.Close();
  87. }
  88. }
  89. catch (Exception e) { }
  90. }
  91. /// <summary>
  92. /// 停止当前进程
  93. /// </summary>
  94. public static void KillCurrent()
  95. {
  96. try
  97. {
  98. Process current = Process.GetCurrentProcess();
  99. current.Kill();
  100. }
  101. catch { }
  102. }
  103. /// <summary>
  104. /// 停止多个进程
  105. /// </summary>
  106. /// <param name="names"></param>
  107. public static void Kills(string[] names)
  108. {
  109. if (ListTool.HasElements(names))
  110. {
  111. foreach (var name in names)
  112. {
  113. if (!string.IsNullOrWhiteSpace(name))
  114. Kill(name);
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// 停止超时进程
  120. /// </summary>
  121. /// <param name="name">进程名(不含后缀)</param>
  122. /// <param name="file">文件路径(为空则不验证)</param>
  123. /// <param name="second">超时时间(单位:秒)</param>
  124. public static void Kills(string name, string file, int second)
  125. {
  126. try
  127. {
  128. Process[] list = Process.GetProcessesByName(name);
  129. if (ListTool.HasElements(list))
  130. {
  131. DateTime time = DateTime.Now.AddSeconds(-1 * second);
  132. foreach (var p in list)
  133. {
  134. if (file == null || p.MainModule.FileName == file)
  135. if (p.StartTime < time)
  136. try { p.Kill(); } catch { }
  137. }
  138. }
  139. }
  140. catch { }
  141. }
  142. /// <summary>
  143. /// 强制关闭超时进程
  144. /// </summary>
  145. /// <param name="process"></param>
  146. /// <param name="second"></param>
  147. public static void Kill(Process process, int second)
  148. {
  149. DateTime time = DateTime.Now.AddSeconds(-1 * second);
  150. if (process.StartTime < time)
  151. try { process.Kill(); } catch { }
  152. }
  153. /// <summary>
  154. /// 延时并关闭进程
  155. /// </summary>
  156. /// <param name="process"></param>
  157. /// <param name="second"></param>
  158. public static void SleepKill(Process process, short second)
  159. {
  160. Task.Factory.StartNew(() =>
  161. {
  162. try
  163. {
  164. Sleep.S(second);
  165. process?.Kill();
  166. }
  167. catch { }
  168. });
  169. }
  170. /// <summary>
  171. /// 根据PID获取InstanceName(不要用于性能计数器,#1..实例名会自动改变)
  172. /// </summary>
  173. /// <param name="id"></param>
  174. /// <returns></returns>
  175. public static string GetInstanceNameById(int id)
  176. {
  177. try
  178. {
  179. PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
  180. string[] instances = cat.GetInstanceNames();
  181. foreach (string instance in instances)
  182. {
  183. try
  184. {
  185. using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
  186. {
  187. int val = (int)cnt.RawValue;
  188. if (val == id)
  189. {
  190. return instance;
  191. }
  192. }
  193. }
  194. catch { }
  195. }
  196. }
  197. catch { }
  198. return null;
  199. }
  200. }
  201. }