C#一键显示及杀死占用端口号进程

参照园友freeliver54代码而做的,再根据需求做的,菜鸟一只有问题尽管提出来
----------------------------------------------------------------------

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

 

posted @ 2015-12-22 11:52  冣綮風  阅读(2071)  评论(0编辑  收藏  举报