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

Whe*_*lie 5 c# tcp

是否有托管类/方法提供特定Windows进程使用的TCP端口号?

我真的在寻找以下CMD系列的.NET等价物:

netstat -ano |find /i "listening"
Run Code Online (Sandbox Code Playgroud)

Rub*_*ias 15

除了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();
Run Code Online (Sandbox Code Playgroud)

来源:C#中的Netstat

更多研究带来了这个:使用c#构建自己的netstat.exe.这使用P/Invoke来调用GetExtendedTcpTable和使用相同的结构netstat.

  • 还要获得PID? (2认同)