ProcessTool.cs 3.3 KB

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