ProcessTool.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. Process[] processes = Process.GetProcessesByName(name);
  24. foreach (Process p in processes)
  25. {
  26. return true;
  27. }
  28. return false;
  29. }
  30. /// <summary>
  31. /// 启动程序
  32. /// </summary>
  33. /// <param name="file">文件路径</param>
  34. /// <param name="args">启动参数</param>
  35. /// <returns></returns>
  36. public static bool Start(string file, string args = "")
  37. {
  38. if (File.Exists(file))
  39. {
  40. try
  41. {
  42. Process.Start(file, args);
  43. return true;
  44. }
  45. catch { }
  46. }
  47. return false;
  48. }
  49. /// <summary>
  50. /// 启动进程(定制启动配置)
  51. /// </summary>
  52. /// <param name="args"></param>
  53. public static bool StartCustom(string file, string args = "")
  54. {
  55. try
  56. {
  57. if (File.Exists(file))
  58. {
  59. Process p = new Process();
  60. p.StartInfo.FileName = file;
  61. p.StartInfo.Arguments = args;
  62. p.StartInfo.UseShellExecute = true;
  63. p.Start();
  64. p.WaitForInputIdle(3000);
  65. return true;
  66. }
  67. }
  68. catch (Exception ex) { }
  69. return false;
  70. }
  71. /// <summary>
  72. /// 停止进程
  73. /// </summary>
  74. /// <param name="name">进程名</param>
  75. public static void Kill(string name)
  76. {
  77. try
  78. {
  79. Process[] processes = Process.GetProcessesByName(name);
  80. foreach (Process p in processes)
  81. {
  82. p.Kill();
  83. p.Close();
  84. }
  85. }
  86. catch (Exception e) { }
  87. }
  88. /// <summary>
  89. /// 停止当前进程
  90. /// </summary>
  91. public static void KillCurrentProcess()
  92. {
  93. Process current = Process.GetCurrentProcess();
  94. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  95. foreach (Process process in processes)
  96. {
  97. if (process.Id == current.Id)
  98. {
  99. process.Kill();
  100. }
  101. }
  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. /// 根据PID获取InstanceName(不要用于性能计数器,#1..实例名会自动改变)
  120. /// </summary>
  121. /// <param name="id"></param>
  122. /// <returns></returns>
  123. public static string GetInstanceNameById(int id)
  124. {
  125. try
  126. {
  127. PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
  128. string[] instances = cat.GetInstanceNames();
  129. foreach (string instance in instances)
  130. {
  131. try
  132. {
  133. using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
  134. {
  135. int val = (int)cnt.RawValue;
  136. if (val == id)
  137. {
  138. return instance;
  139. }
  140. }
  141. }
  142. catch { }
  143. }
  144. }
  145. catch { }
  146. return null;
  147. }
  148. }
  149. }