ProcessTool.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.DataUtils.CollectionUtils;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. namespace Azylee.Core.ProcessUtils
  13. {
  14. public static class ProcessTool
  15. {
  16. public static void StartProcess(string appFile)
  17. {
  18. try
  19. {
  20. if (File.Exists(appFile))
  21. {
  22. Process p = new Process();
  23. p.StartInfo.FileName = appFile;
  24. //p.StartInfo.Arguments = "";
  25. p.StartInfo.UseShellExecute = true;
  26. p.Start();
  27. p.WaitForInputIdle(3000);
  28. }
  29. }
  30. catch (Exception ex) { }
  31. }
  32. public static bool CheckProcessExists(string name)
  33. {
  34. Process[] processes = Process.GetProcessesByName(name);
  35. foreach (Process p in processes)
  36. {
  37. return true;
  38. }
  39. return false;
  40. }
  41. public static void KillProcess(string name)
  42. {
  43. try
  44. {
  45. Process[] processes = Process.GetProcessesByName(name);
  46. foreach (Process p in processes)
  47. {
  48. p.Kill();
  49. p.Close();
  50. }
  51. }
  52. catch (Exception e) { }
  53. }
  54. public static void KillCurrentProcess()
  55. {
  56. Process current = Process.GetCurrentProcess();
  57. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  58. foreach (Process process in processes)
  59. {
  60. if (process.Id == current.Id)
  61. {
  62. process.Kill();
  63. }
  64. }
  65. }
  66. public static bool Start(string file, string args = "")
  67. {
  68. try
  69. {
  70. if (File.Exists(file))
  71. {
  72. Process p = new Process();
  73. p.StartInfo.FileName = file;
  74. p.StartInfo.Arguments = "";
  75. p.StartInfo.UseShellExecute = true;
  76. p.Start();
  77. p.WaitForInputIdle(3000);
  78. return true;
  79. }
  80. }
  81. catch (Exception ex) { }
  82. return false;
  83. }
  84. public static bool SimpleStart(string file, string args = "")
  85. {
  86. if (File.Exists(file))
  87. {
  88. try
  89. {
  90. Process.Start(file, args);
  91. return true;
  92. }
  93. catch { }
  94. }
  95. return false;
  96. }
  97. public static void Starts(string[] files)
  98. {
  99. if (ListTool.HasElements(files))
  100. {
  101. foreach (var f in files)
  102. {
  103. if (!string.IsNullOrWhiteSpace(f))
  104. StartProcess(f);
  105. }
  106. }
  107. }
  108. public static void Kills(string[] pro)
  109. {
  110. if (ListTool.HasElements(pro))
  111. {
  112. foreach (var p in pro)
  113. {
  114. if (!string.IsNullOrWhiteSpace(p))
  115. KillProcess(p);
  116. }
  117. }
  118. }
  119. }
  120. }