【发布时间】:2010-12-21 14:52:44
【问题描述】:
是否有可提供特定 Windows 进程使用的 TCP 端口号的托管类/方法?
我真的在寻找与以下 CMD 行等效的 .NET:
netstat -ano |find /i "listening"
【问题讨论】:
是否有可提供特定 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();
更多研究带来这个:Build your own netstat.exe with c#。这使用 P/Invoke 调用 GetExtendedTcpTable 并使用与 netstat 相同的结构。
【讨论】:
在此处查看 C# 中的 netstat 等效项:http://towardsnext.wordpess.com/2009/02/09/netstat-in-c/
更新:链接已损坏,但这里有一个等价的:http://www.timvw.be/2007/09/09/build-your-own-netstatexe-with-c
更新:原始页面已存档在Wayback Machine。
【讨论】: