ProcessTool.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, string args = "")
  69. {
  70. try
  71. {
  72. if (File.Exists(file))
  73. {
  74. Process p = new Process();
  75. p.StartInfo.FileName = file;
  76. p.StartInfo.Arguments = "";
  77. p.StartInfo.UseShellExecute = true;
  78. p.Start();
  79. p.WaitForInputIdle(3000);
  80. return true;
  81. }
  82. }
  83. catch (Exception ex) { }
  84. return false;
  85. }
  86. public static void Starts(string[] files)
  87. {
  88. if (ListTool.HasElements(files))
  89. {
  90. foreach (var f in files)
  91. {
  92. if (!string.IsNullOrWhiteSpace(f))
  93. StartProcess(f);
  94. }
  95. }
  96. }
  97. public static void Kills(string[] pro)
  98. {
  99. if (ListTool.HasElements(pro))
  100. {
  101. foreach (var p in pro)
  102. {
  103. if (!string.IsNullOrWhiteSpace(p))
  104. KillProcess(p);
  105. }
  106. }
  107. }
  108. }
  109. }