ProcessTool.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using Y.Utils.DataUtils.Collections;
  11. namespace Y.Utils.WindowsUtils.ProcessUtils
  12. {
  13. public static class ProcessTool
  14. {
  15. public static void StartProcess(string appFile)
  16. {
  17. try
  18. {
  19. if (File.Exists(appFile))
  20. {
  21. Process p = new Process();
  22. p.StartInfo.FileName = appFile;
  23. //p.StartInfo.Arguments = "";
  24. p.StartInfo.UseShellExecute = true;
  25. p.Start();
  26. p.WaitForInputIdle(3000);
  27. }
  28. }
  29. catch (Exception ex) { }
  30. }
  31. public static bool CheckProcessExists(string name)
  32. {
  33. Process[] processes = Process.GetProcessesByName(name);
  34. foreach (Process p in processes)
  35. {
  36. return true;
  37. }
  38. return false;
  39. }
  40. public static void KillProcess(string name)
  41. {
  42. try
  43. {
  44. Process[] processes = Process.GetProcessesByName(name);
  45. if (ListTool.HasElements(processes))
  46. {
  47. foreach (Process p in processes)
  48. {
  49. p.Kill();
  50. p.Close();
  51. }
  52. }
  53. }
  54. catch (Exception e) { }
  55. }
  56. public static void KillCurrentProcess()
  57. {
  58. Process current = Process.GetCurrentProcess();
  59. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  60. foreach (Process process in processes)
  61. {
  62. if (process.Id == current.Id)
  63. {
  64. process.Kill();
  65. }
  66. }
  67. }
  68. public static bool Start(string file)
  69. {
  70. try
  71. {
  72. if (File.Exists(file))
  73. {
  74. Process p = new Process();
  75. p.StartInfo.FileName = file;
  76. p.Start();
  77. return true;
  78. }
  79. }
  80. catch (Exception ex) { }
  81. return false;
  82. }
  83. public static bool Start(string file, string args)
  84. {
  85. try
  86. {
  87. if (File.Exists(file))
  88. {
  89. Process p = new Process();
  90. p.StartInfo.FileName = file;
  91. p.StartInfo.Arguments = args;
  92. p.StartInfo.UseShellExecute = true;
  93. p.Start();
  94. p.WaitForInputIdle(3000);
  95. return true;
  96. }
  97. }
  98. catch (Exception ex) { }
  99. return false;
  100. }
  101. public static void Starts(string[] files)
  102. {
  103. if (ListTool.HasElements(files))
  104. {
  105. foreach (var f in files)
  106. {
  107. if (!string.IsNullOrWhiteSpace(f))
  108. StartProcess(f);
  109. }
  110. }
  111. }
  112. public static void Kills(string[] pro)
  113. {
  114. if (ListTool.HasElements(pro))
  115. {
  116. foreach (var p in pro)
  117. {
  118. if (!string.IsNullOrWhiteSpace(p))
  119. KillProcess(p);
  120. }
  121. }
  122. }
  123. }
  124. }