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 == )
{
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 > && ((strLine.Contains("TCP") || strLine.Contains("UDP"))))
{
Regex r = new Regex(@"\s+");
string[] strArr = r.Split(strLine);
if (strArr.Length >= )
{
b = int.TryParse(strArr[], 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 > && ((strLine.Contains(".exe"))))
{
Regex r = new Regex(@"\s+");
string[] strArr = r.Split(strLine);
if (strArr.Length > )
{
list_process.Add(strArr[]);
}
}
strLine = reader.ReadLine();
}
p.WaitForExit();
reader.Close();
}
p.Close(); return list_process;
}

原文链接:C#一键显示及杀死占用端口号进程

参考文献:C#实现检查指定端口被哪个进程占用

C#一键显示及杀死占用端口号进程的更多相关文章

  1. windows查看和杀死占用端口的进程

    1.首先使用 netstat -ano查看占用端口的进程号 2.然后使用 taskkill /PID (进程号)杀死进程

  2. windows环境下 快速杀死占用端口的进程

    保存为bat脚本,设置需要解除占用的端口 port,点击运行即可 @echo off setlocal enabledelayedexpansion set prot = 8022 for /f &q ...

  3. 结束占用端口号进程(pid)

  4. linux与windows查看占用端口的进程ID并杀死进程

    有时候tomcat出现端口被占用,需要查出进程ID并杀死进程. 1.查找占用端口的进程ID(windows与linux一样  8005也可以加上引号   grep可以用findstr替换) 6904就 ...

  5. Linux 一条命令杀死占用端口的所有进程

    Linux 一条命令杀死占用端口的所有进程 2018年05月28日 19:43:05 gq97 阅读数 7655更多 分类专栏: Linux   版权声明:本文为博主原创文章,遵循CC 4.0 BY- ...

  6. Windows中杀死某个端口的进程

    最近写项目,总是出现端口被占用的问题,原来傻傻的把电脑重启一下,终于有一天受不了了,想要想办法解决.刚开始从网上找了好多教程,发现不行.开始自己尝试,终于,成功的将占用端口的进程杀掉.在此记录下过程( ...

  7. Linux及Windows查看占用端口的进程

    想必大家在部署环境启动服务的时候,会遇到服务起不起来的问题,看日志,说是端口被占用了. 有的时候,我们不想改端口,那么,就需要去查看到底是哪个应用把这个端口给占用了,然后干掉它即可. 下面分别列举li ...

  8. Windows系统如何找到占用端口的进程并杀掉

    1.先建立用户环境变量:C\WINDOWS/system32 2.输入:cmd,打开命令控制台,然后输入ipconfig 3.再输入:netstat -ano(可以找到所有的进程连接端口及对应PID) ...

  9. windows下端口占用解决方法-查看和杀死占用端口进程

    在Windows下启动程序时有时会遇到端口被占用的情况,由于一个端口同时只能运行一个进程,所以要想启动新的程序就要先把占用该端口的进程给kill掉,具体的命令分为以下三步, 以杀死占用了80端口的进程 ...

随机推荐

  1. COGS2485 从零开始的序列

    传送门 题意:给定一个长为$n$的序列,定义$f(x)$表示所有(长为$x$的区间最小值)的最大值,求$f(1)$~$f(n)$. 看好多人都用并查集做的,然而我并不想写……既然品酒大会可以后缀数组+ ...

  2. pv-date-handle

    pv-date-handle 这是一个时间格式化转换的小工具,简单易用 ## 快速开始 安装`npm install pv-date-handle -S` ```js import formateDa ...

  3. CSS3,3D效果轮播图

    ---恢复内容开始--- 大家还记得我昨天的3D拖拽立方体吗??我昨天还说过css还可以做轮播图,所以咱们今天就写一下,css的轮播图吧! ....这个轮播图主要是用CSS3里的transform的旋 ...

  4. easyui window窗口 不垂直居中

    $('#hotelDetailWrap').window({ width: 1040, height: 160, collapsible: false, minimizable: false, max ...

  5. 数据结构----线性表顺序和链式结构的使用(c)

    PS:在学习数据结构之前,我相信很多博友也都学习过一些语言,比如说java,c语言,c++,web等,我们之前用的一些方法大都是封装好的,就java而言,里面使用了大量的封装好的方法,一些算法也大都写 ...

  6. 关于建立Android工程R文件丢失的问题

    今天开始学习Android了,好久没打开eclipse,建立Android工程老是报错,于是手残的把appcompat-v7给删了,然后建立工程以后重新出来的appcompat-v7有个小叉号,百度了 ...

  7. DDMS 使用小结

    DDMS简介 DDMS功能详解 1 Devices面板 2 功能面板 3 LogCat and Console 1.DDMS简介 DDMS的全称是Dalvik Debug Monitor Servic ...

  8. python链接安卓 跳一跳

    借鉴:  一百行代码实现微信跳一跳 之前需要安装adb 开启服务: adb nodaemon server 显示设备: adb devices 代码: import math import os im ...

  9. golang构造函数

    http://blog.jobbole.com/107442/?utm_source=blog.jobbole.com&utm_medium=relatedPosts https://gocn ...

  10. C++程序运行时间-ZZ

    [15.5.25]贴一段实用的代码,linux下跑过. #include <stdio.h> /* printf */ #include <time.h> /* clock_t ...