Program.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace HTEXKiller
  8. {
  9. class Program
  10. {
  11. /// <summary>
  12. /// kill指定端口的PID进程。
  13. /// </summary>
  14. /// <param name="args"></param>
  15. static void Main(string[] args)
  16. {
  17. int port = 9527;
  18. Process p = new Process();
  19. p.StartInfo.FileName = "cmd.exe";
  20. p.StartInfo.UseShellExecute = false;
  21. p.StartInfo.RedirectStandardError = true;
  22. p.StartInfo.RedirectStandardInput = true;
  23. p.StartInfo.RedirectStandardOutput = true;
  24. p.StartInfo.CreateNoWindow = true;
  25. List<int> list_pid = GetPidByPort(p, port);
  26. List<string> list_process = GetProcessNameByPid(p, list_pid);
  27. StringBuilder sb = new StringBuilder();
  28. sb.AppendLine("占用" + port + "端口的进程有:");
  29. foreach (var item in list_process)
  30. {
  31. sb.Append(item + "\r\n");
  32. }
  33. sb.AppendLine("是否要结束这些进程?");
  34. Console.WriteLine(sb);
  35. PidKill(p, list_pid);
  36. Console.ReadLine();
  37. }
  38. private static void PidKill(Process p, List<int> list_pid)
  39. {
  40. p.Start();
  41. foreach (var item in list_pid)
  42. {
  43. p.StandardInput.WriteLine("taskkill /pid " + item + " /f");
  44. p.StandardInput.WriteLine("exit");
  45. }
  46. p.Close();
  47. }
  48. private static List<int> GetPidByPort(Process p, int port)
  49. {
  50. int result;
  51. bool b = true;
  52. p.Start();
  53. p.StandardInput.WriteLine(string.Format("netstat -ano|find \"{0}\"", port));
  54. p.StandardInput.WriteLine("exit");
  55. StreamReader reader = p.StandardOutput;
  56. string strLine = reader.ReadLine();
  57. List<int> list_pid = new List<int>();
  58. StringBuilder sb = new StringBuilder();
  59. while (!reader.EndOfStream)
  60. {
  61. strLine = strLine.Trim();
  62. if (strLine.Length > 0 && ((strLine.Contains("TCP") || strLine.Contains("UDP"))))
  63. {
  64. Regex r = new Regex(@"\s+");
  65. string[] strArr = r.Split(strLine);
  66. for (int i = 2; i < strArr.Length; i++) {
  67. b = int.TryParse(strArr[i], out result);
  68. if (b && !list_pid.Contains(result))
  69. list_pid.Add(result);
  70. }
  71. }
  72. strLine = reader.ReadLine();
  73. sb.Append(strLine);
  74. }
  75. p.WaitForExit();
  76. reader.Close();
  77. p.Close();
  78. return list_pid;
  79. }
  80. private static List<string> GetProcessNameByPid(Process p, List<int> list_pid)
  81. {
  82. p.Start();
  83. List<string> list_process = new List<string>();
  84. StreamReader reader = null;
  85. foreach (var pid in list_pid)
  86. {
  87. p.StandardInput.WriteLine(string.Format("tasklist |find \"{0}\"", pid));
  88. p.StandardInput.WriteLine("exit");
  89. reader = p.StandardOutput;//截取输出流
  90. string strLine = reader.ReadLine();//每次读取一行
  91. while (!reader.EndOfStream)
  92. {
  93. strLine = strLine.Trim();
  94. if (strLine.Length > 0 && ((strLine.Contains(".exe"))))
  95. {
  96. Regex r = new Regex(@"\s+");
  97. string[] strArr = r.Split(strLine);
  98. if (strArr.Length > 0)
  99. {
  100. list_process.Add(strArr[0]);
  101. }
  102. }
  103. strLine = reader.ReadLine();
  104. }
  105. p.WaitForExit();
  106. }
  107. if (reader != null) {
  108. reader.Close();
  109. }
  110. p.Close();
  111. return list_process;
  112. }
  113. }
  114. }