|
|
|
问:用C#写一个程序 列出指定程序的网络连接
答:(我截取主要部分的代码)
Process[] processes = Process.GetProcessesByName("notepad");
if (processes.Length > 0)
{
foreach (Process process in processes)
{
Console.WriteLine("Process name: {0}", process.ProcessName);
Console.WriteLine("Process ID: {0}", process.Id);
Console.WriteLine("Network connections:");
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation tcp in tcpConnections)
{
if (tcp.ProcessId == process.Id) //注意这里,微软官网最新的文档里面tcp并没有ProcessId这个属性了
{
Console.WriteLine("{0} <==> {1}", tcp.localEndPoint.ToString(), tcp.RemoteEndPoint.ToString());
}
}
}
}
else
{
Console.WriteLine("Process not found.");
}
这个是微软官网的TcpConnectionInformation文档
https://learn.microsoft.com/zh-c ... =netframework-4.7.2
可以看到里面就3个属性,根本没有ProcessId这个属性 |
|