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 == )
{
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#一键显示及杀死占用端口号进程的更多相关文章
- windows查看和杀死占用端口的进程
1.首先使用 netstat -ano查看占用端口的进程号 2.然后使用 taskkill /PID (进程号)杀死进程
- windows环境下 快速杀死占用端口的进程
保存为bat脚本,设置需要解除占用的端口 port,点击运行即可 @echo off setlocal enabledelayedexpansion set prot = 8022 for /f &q ...
- 结束占用端口号进程(pid)
- linux与windows查看占用端口的进程ID并杀死进程
有时候tomcat出现端口被占用,需要查出进程ID并杀死进程. 1.查找占用端口的进程ID(windows与linux一样 8005也可以加上引号 grep可以用findstr替换) 6904就 ...
- Linux 一条命令杀死占用端口的所有进程
Linux 一条命令杀死占用端口的所有进程 2018年05月28日 19:43:05 gq97 阅读数 7655更多 分类专栏: Linux 版权声明:本文为博主原创文章,遵循CC 4.0 BY- ...
- Windows中杀死某个端口的进程
最近写项目,总是出现端口被占用的问题,原来傻傻的把电脑重启一下,终于有一天受不了了,想要想办法解决.刚开始从网上找了好多教程,发现不行.开始自己尝试,终于,成功的将占用端口的进程杀掉.在此记录下过程( ...
- Linux及Windows查看占用端口的进程
想必大家在部署环境启动服务的时候,会遇到服务起不起来的问题,看日志,说是端口被占用了. 有的时候,我们不想改端口,那么,就需要去查看到底是哪个应用把这个端口给占用了,然后干掉它即可. 下面分别列举li ...
- Windows系统如何找到占用端口的进程并杀掉
1.先建立用户环境变量:C\WINDOWS/system32 2.输入:cmd,打开命令控制台,然后输入ipconfig 3.再输入:netstat -ano(可以找到所有的进程连接端口及对应PID) ...
- windows下端口占用解决方法-查看和杀死占用端口进程
在Windows下启动程序时有时会遇到端口被占用的情况,由于一个端口同时只能运行一个进程,所以要想启动新的程序就要先把占用该端口的进程给kill掉,具体的命令分为以下三步, 以杀死占用了80端口的进程 ...
随机推荐
- 极客DIY:打造你的专属黑客U盘
简介 由于“Bad USB漏洞”的存在,USB闪存驱动器也成了常见的攻击目标.Bad-USB让黑客可以重新编程微控器作为一个“人机界面装置”(HID)或键盘,然后在目标机器上执行自定义键盘敲击.这种情 ...
- android 界面设计基本知识Ⅱ
上一章讲述了Android界面设计时,一些基本控件的使用,本章主要讲述自定义控件,Fragment和Headler线程机制. 1.自定义控件 (1)基本知识 dp.sp和dx px:像素点 ...
- 使用Java的嵌套循环打印出平行四边形、等腰三角形、棱形、矩形的星星图案(Java工程师面试必备)
第一遍是看了视频,听老师讲解嵌套循环的使用,然后到星星图形这一步,当时都觉得听明白了,但是自己去做,就是写不出来 第二遍看了赵老师的教程,看了好熟悉的感觉,还是自己写不出来 第三遍找网上关于图形的嵌套 ...
- poj 3750 小孩报数问题 解题报告
题目链接:http://poj.org/problem?id=3750 约瑟夫问题,直接模拟即可. #include <iostream> #include <string> ...
- Http post请求数据分析 --作者, 你的这个需求我可以做, 我在平台上无法给你发消息和接收你的任务, 所以,如果你看到这个信息, 可以联系我.
Http post请求数据分析 作者, 你的这个需求我可以做, 我在平台上无法给你发消息和接收你的任务, 所以,如果你看到这个信息, 可以联系我. 软件需求就是不停post一个网址,然后根据返回的信息 ...
- mysql的查询
1.单表查询 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 ...
- c#winform,知道图像路径,怎么在程序运行时往image里面添加图片
貌似可以直接添加啊 ,要改变显示的图片,就是将图片的路径赋值给picturebox即可pictureBox1.ImageLocation="图片路径"动态的改变这个值就行了.
- 关于Session的概念和测试点
Session概要 Session 是用于保持状态的基于 Web 服务器的方法,在 Web 服务器上保持用户的状态信息供在任何时间从任何页访问. Session 允许通过将对象存储在 Web 服务器的 ...
- 深度学习基础系列(十)| Global Average Pooling是否可以替代全连接层?
Global Average Pooling(简称GAP,全局池化层)技术最早提出是在这篇论文(第3.2节)中,被认为是可以替代全连接层的一种新技术.在keras发布的经典模型中,可以看到不少模型甚至 ...
- poj 2195 Going Home(最小费最大流)
poj 2195 Going Home Description On a grid map there are n little men and n houses. In each unit time ...