ProcessTool.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. namespace Y.Utils.WindowsUtils.ProcessUtils
  11. {
  12. public static class ProcessTool
  13. {
  14. public static void StartProcess(string appFile)
  15. {
  16. try
  17. {
  18. if (File.Exists(appFile))
  19. {
  20. Process p = new Process();
  21. p.StartInfo.FileName = appFile;
  22. //p.StartInfo.Arguments = "";
  23. p.StartInfo.UseShellExecute = true;
  24. p.Start();
  25. p.WaitForInputIdle(3000);
  26. }
  27. }
  28. catch (Exception ex) { }
  29. }
  30. public static bool CheckProcessExists(string name)
  31. {
  32. Process[] processes = Process.GetProcessesByName(name);
  33. foreach (Process p in processes)
  34. {
  35. return true;
  36. }
  37. return false;
  38. }
  39. public static void KillProcess(string name)
  40. {
  41. try
  42. {
  43. Process[] processes = Process.GetProcessesByName(name);
  44. foreach (Process p in processes)
  45. {
  46. p.Kill();
  47. p.Close();
  48. }
  49. }
  50. catch (Exception e) { }
  51. }
  52. public static void KillCurrentProcess()
  53. {
  54. Process current = Process.GetCurrentProcess();
  55. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  56. foreach (Process process in processes)
  57. {
  58. if (process.Id == current.Id)
  59. {
  60. process.Kill();
  61. }
  62. }
  63. }
  64. }
  65. }