C#一键显示及杀死占用端口号进程
private void t_btn_kill_Click(object sender, EventArgs e) { int port; bool b = int.TryParse(t_txt_guardport.Text, out port); if (!b) { MessageBox.Show("请输入正确的监听端口"); return; } Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardError = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; List<int> list_pid = GetPidByPort(p, port); if (list_pid.Count == 0) { MessageBox.Show("操作完成"); return; } List<string> list_process = GetProcessNameByPid(p, list_pid); StringBuilder sb = new StringBuilder(); sb.AppendLine("占用" + port + "端口的进程有:"); foreach (var item in list_process) { sb.Append(item + "\r\n"); } sb.AppendLine("是否要结束这些进程?"); if (MessageBox.Show(sb.ToString(), "系统提示", MessageBoxButtons.YesNo) == DialogResult.No) return; PidKill(p, list_pid); MessageBox.Show("操作完成"); } private static void PidKill(Process p, List<int> list_pid) { p.Start(); foreach (var item in list_pid) { p.StandardInput.WriteLine("taskkill /pid " + item + " /f"); p.StandardInput.WriteLine("exit"); } p.Close(); } private static List<int> GetPidByPort(Process p, int port) { int result; bool b = true; p.Start(); p.StandardInput.WriteLine(string.Format("netstat -ano|find \"{0}\"", port)); p.StandardInput.WriteLine("exit"); StreamReader reader = p.StandardOutput; string strLine = reader.ReadLine(); List<int> list_pid = new List<int>(); while (!reader.EndOfStream) { strLine = strLine.Trim(); if (strLine.Length > 0 && ((strLine.Contains("TCP") || strLine.Contains("UDP")))) { Regex r = new Regex(@"\s+"); string[] strArr = r.Split(strLine); if (strArr.Length >= 4) { b = int.TryParse(strArr[3], out result); if (b && !list_pid.Contains(result)) list_pid.Add(result); } } strLine = reader.ReadLine(); } p.WaitForExit(); reader.Close(); p.Close(); return list_pid; } private static List<string> GetProcessNameByPid(Process p, List<int> list_pid) { p.Start(); List<string> list_process = new List<string>(); foreach (var pid in list_pid) { p.StandardInput.WriteLine(string.Format("tasklist |find \"{0}\"", pid)); p.StandardInput.WriteLine("exit"); StreamReader reader = p.StandardOutput;//截取输出流 string strLine = reader.ReadLine();//每次读取一行 while (!reader.EndOfStream) { strLine = strLine.Trim(); if (strLine.Length > 0 && ((strLine.Contains(".exe")))) { Regex r = new Regex(@"\s+"); string[] strArr = r.Split(strLine); if (strArr.Length > 0) { list_process.Add(strArr[0]); } } strLine = reader.ReadLine(); } p.WaitForExit(); reader.Close(); } p.Close(); return list_process; }
原文链接:C#一键显示及杀死占用端口号进程
参考文献:C#实现检查指定端口被哪个进程占用
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理