ProcessTool.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //************************************************************************
  2. // https://github.com/yuzhengyang
  3. // author: yuzhengyang
  4. // date: 2017.10.12 - 2018.5.02
  5. // desc: 进程工具
  6. // Copyright (c) yuzhengyang. All rights reserved.
  7. //************************************************************************
  8. using Azylee.Core.DataUtils.CollectionUtils;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. namespace Azylee.Core.ProcessUtils
  13. {
  14. public static class ProcessTool
  15. {
  16. /// <summary>
  17. /// 判断进程是否存在
  18. /// </summary>
  19. /// <param name="name">进程名</param>
  20. /// <returns></returns>
  21. public static bool IsExists(string name)
  22. {
  23. try
  24. {
  25. var p = Process.GetProcessesByName(name);
  26. if (ListTool.HasElements(p)) return true;
  27. }
  28. catch { }
  29. return false;
  30. }
  31. /// <summary>
  32. /// 启动程序
  33. /// </summary>
  34. /// <param name="file">文件路径</param>
  35. /// <param name="args">启动参数</param>
  36. /// <returns></returns>
  37. public static bool Start(string file, string args = "")
  38. {
  39. if (File.Exists(file))
  40. {
  41. try
  42. {
  43. Process.Start(file, args);
  44. return true;
  45. }
  46. catch { }
  47. }
  48. return false;
  49. }
  50. /// <summary>
  51. /// 启动进程(定制启动配置)
  52. /// </summary>
  53. /// <param name="args"></param>
  54. public static bool StartCustom(string file, string args = "")
  55. {
  56. try
  57. {
  58. if (File.Exists(file))
  59. {
  60. Process p = new Process();
  61. p.StartInfo.FileName = file;
  62. p.StartInfo.Arguments = args;
  63. p.StartInfo.UseShellExecute = true;
  64. p.Start();
  65. p.WaitForInputIdle(3000);
  66. return true;
  67. }
  68. }
  69. catch (Exception ex) { }
  70. return false;
  71. }
  72. /// <summary>
  73. /// 停止进程
  74. /// </summary>
  75. /// <param name="name">进程名</param>
  76. public static void Kill(string name)
  77. {
  78. try
  79. {
  80. Process[] processes = Process.GetProcessesByName(name);
  81. foreach (Process p in processes)
  82. {
  83. p.Kill();
  84. p.Close();
  85. }
  86. }
  87. catch (Exception e) { }
  88. }
  89. /// <summary>
  90. /// 停止当前进程
  91. /// </summary>
  92. public static void KillCurrentProcess()
  93. {
  94. Process current = Process.GetCurrentProcess();
  95. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  96. foreach (Process process in processes)
  97. {
  98. if (process.Id == current.Id)
  99. {
  100. process.Kill();
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 停止多个进程
  106. /// </summary>
  107. /// <param name="names"></param>
  108. public static void Kills(string[] names)
  109. {
  110. if (ListTool.HasElements(names))
  111. {
  112. foreach (var name in names)
  113. {
  114. if (!string.IsNullOrWhiteSpace(name))
  115. Kill(name);
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 根据PID获取InstanceName(不要用于性能计数器,#1..实例名会自动改变)
  121. /// </summary>
  122. /// <param name="id"></param>
  123. /// <returns></returns>
  124. public static string GetInstanceNameById(int id)
  125. {
  126. try
  127. {
  128. PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
  129. string[] instances = cat.GetInstanceNames();
  130. foreach (string instance in instances)
  131. {
  132. try
  133. {
  134. using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
  135. {
  136. int val = (int)cnt.RawValue;
  137. if (val == id)
  138. {
  139. return instance;
  140. }
  141. }
  142. }
  143. catch { }
  144. }
  145. }
  146. catch { }
  147. return null;
  148. }
  149. }
  150. }