做了个WinForm,想实现输入端口号,查询指定端口号的进程,然后终止进程。参照了网上的示例,读端口号结果,获取pid时发生错误
故障现象 如果指定为21 80 这样的端口,就会出现
System.ObjectDisposedException: 无法从已关闭的 TextReader 中读取。这个错误。
未占用的端口,运行结果正常。
22 这样的端口,又正常 提示为svchost.exe在占用。
请教各位应该如何解决。
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;
}
错误为: