ProcessHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. namespace Vinno.FIS.Sonopost.Common
  7. {
  8. public class ProcessHelper
  9. {
  10. /// <summary>
  11. /// 开启进程
  12. /// </summary>
  13. /// <param name="processPath"></param>
  14. /// <param name="args"></param>
  15. /// <returns></returns>
  16. public static int StartProcess(string processPath, string args = "")
  17. {
  18. if (!File.Exists(processPath))
  19. {
  20. return 0;
  21. }
  22. int retryCount = 0;
  23. int processId;
  24. bool exist;
  25. do
  26. {
  27. var p = new Process
  28. {
  29. StartInfo =
  30. {
  31. FileName = processPath,
  32. CreateNoWindow = true,
  33. WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
  34. UseShellExecute = false,
  35. WindowStyle = ProcessWindowStyle.Hidden,
  36. RedirectStandardInput = true,
  37. Verb = "runas",
  38. Arguments = args
  39. }
  40. };
  41. p.Start();
  42. processId = p.Id;
  43. var existApp = IsProcessExist(processId);
  44. exist = existApp != null;
  45. if (exist == false && retryCount < 5)
  46. {
  47. retryCount++;
  48. Thread.Sleep(1000);
  49. }
  50. } while (exist == false);
  51. return processId;
  52. }
  53. /// <summary>
  54. /// 结束进程
  55. /// </summary>
  56. /// <param name="processId"></param>
  57. /// <returns></returns>
  58. public static bool FinishProcess(int processId)
  59. {
  60. int count = 10;
  61. while (count > 0)
  62. {
  63. var existApp = IsProcessExist(processId);
  64. if (existApp != null)
  65. {
  66. count--;
  67. try
  68. {
  69. existApp.Kill();
  70. }
  71. catch
  72. {
  73. }
  74. Thread.Sleep(100);
  75. }
  76. else
  77. {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /// <summary>
  84. /// 结束所有这名称进程
  85. /// </summary>
  86. /// <param name="name"></param>
  87. /// <returns></returns>
  88. public static bool FinishProcessByName(string processName)
  89. {
  90. int count = 10;
  91. while (count > 0)
  92. {
  93. var existApps = Process.GetProcessesByName(processName).Where(x => x.ProcessName == processName);
  94. if (!existApps.Any())
  95. {
  96. return true;
  97. }
  98. foreach (var item in existApps)
  99. {
  100. try
  101. {
  102. item.Kill();
  103. }
  104. catch
  105. {
  106. }
  107. Thread.Sleep(100);
  108. }
  109. count--;
  110. }
  111. return false;
  112. }
  113. /// <summary>
  114. /// 检查进程是否存在
  115. /// </summary>
  116. /// <param name="processId"></param>
  117. /// <returns></returns>
  118. public static Process IsProcessExist(int processId)
  119. {
  120. try
  121. {
  122. var existApp = Process.GetProcessById(processId);
  123. if (existApp != null)
  124. {
  125. return existApp;
  126. }
  127. }
  128. catch (ArgumentException)
  129. {
  130. //ignore
  131. }
  132. return null;
  133. }
  134. /// <summary>
  135. /// 检查进程是否存在
  136. /// </summary>
  137. /// <param name="processName"></param>
  138. /// <returns></returns>
  139. public static bool IsProcessExistByName(string processName)
  140. {
  141. return Process.GetProcessesByName(processName).Any(x => x.ProcessName == processName);
  142. }
  143. /// <summary>
  144. /// 根据进程名得到唯一的进程Id
  145. /// </summary>
  146. /// <param name="processName"></param>
  147. /// <returns></returns>
  148. public static int GetProcessIdByName(string processName)
  149. {
  150. var existProcesses = Process.GetProcessesByName(processName).Where(x => x.ProcessName == processName);
  151. if (existProcesses.Count() == 1)
  152. {
  153. return existProcesses.FirstOrDefault().Id;
  154. }
  155. else
  156. {
  157. return 0;
  158. }
  159. }
  160. }
  161. }