有没有一种受控的类/方法可以提供特定Windows进程使用的TCP端口号?
我真正需要的是以下CMD行的.NET等效版本:
netstat -ano |find /i "listening"
除 PID 外,看看这个:
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners();
TcpConnectionInformation[] tcpConnections =
ipProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation info in tcpConnections)
{
Console.WriteLine("Local: {0}:{1}\nRemote: {2}:{3}\nState: {4}\n",
info.LocalEndPoint.Address, info.LocalEndPoint.Port,
info.RemoteEndPoint.Address, info.RemoteEndPoint.Port,
info.State.ToString());
}
Console.ReadLine();
来源: 使用C#编写Netstat
更深入的研究带来了这个: 使用C#构建你自己的netstat.exe。 这个项目使用P/Invoke调用GetExtendedTcpTable
,并使用与netstat
相同的结构。