如何在C#中确定Windows进程使用的tcp端口

是否有托管类/方法可以提供特定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相同的结构。

请参阅此处了解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