ProcessTool.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. foreach (Process p in processes)
  46. {
  47. p.Kill();
  48. p.Close();
  49. }
  50. }
  51. catch (Exception e) { }
  52. }
  53. public static void KillCurrentProcess()
  54. {
  55. Process current = Process.GetCurrentProcess();
  56. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  57. foreach (Process process in processes)
  58. {
  59. if (process.Id == current.Id)
  60. {
  61. process.Kill();
  62. }
  63. }
  64. }
  65. public static void Starts(string[] files)
  66. {
  67. if (ListTool.HasElements(files))
  68. {
  69. foreach (var f in files)
  70. {
  71. if (!string.IsNullOrWhiteSpace(f))
  72. StartProcess(f);
  73. }
  74. }
  75. }
  76. public static void Kills(string[] pro)
  77. {
  78. if (ListTool.HasElements(pro))
  79. {
  80. foreach (var p in pro)
  81. {
  82. if (!string.IsNullOrWhiteSpace(p))
  83. KillProcess(p);
  84. }
  85. }
  86. }
  87. }
  88. }