ProcessTool.cs 6.2 KB

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