ProcessTool.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. namespace Y.Utils.WindowsUtils.ProcessUtils
  10. {
  11. public static class ProcessTool
  12. {
  13. public static void StartProcess(string appFile)
  14. {
  15. try
  16. {
  17. if (File.Exists(appFile))
  18. {
  19. Process p = new Process();
  20. p.StartInfo.FileName = appFile;
  21. //p.StartInfo.Arguments = "";
  22. p.StartInfo.UseShellExecute = true;
  23. p.Start();
  24. p.WaitForInputIdle(3000);
  25. }
  26. }
  27. catch (Exception ex) { }
  28. }
  29. public static bool CheckProcessExists(string name)
  30. {
  31. Process[] processes = Process.GetProcessesByName(name);
  32. foreach (Process p in processes)
  33. {
  34. return true;
  35. }
  36. return false;
  37. }
  38. public static void KillProcess(string name)
  39. {
  40. try
  41. {
  42. Process[] processes = Process.GetProcessesByName(name);
  43. foreach (Process p in processes)
  44. {
  45. p.Kill();
  46. p.Close();
  47. }
  48. }
  49. catch (Exception e) { }
  50. }
  51. public static void KillCurrentProcess()
  52. {
  53. Process current = Process.GetCurrentProcess();
  54. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  55. foreach (Process process in processes)
  56. {
  57. if (process.Id == current.Id)
  58. {
  59. process.Kill();
  60. }
  61. }
  62. }
  63. }
  64. }