• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

参照园友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         }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#图像呈现控件PictureBox发布时间:2022-07-10
下一篇:
通过WebBrowser网页截图C#源码(抓取完整页面及首屏)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap