【问题标题】:How to determine tcp port used by Windows process in C#如何在 C# 中确定 Windows 进程使用的 tcp 端口
【发布时间】:2010-12-21 14:52:44
【问题描述】:

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

我真的在寻找与以下 CMD 行等效的 .NET:

netstat -ano |find /i "listening"

【问题讨论】:

    标签: c# tcp


    【解决方案1】:

    除了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();
    

    来源:Netstat in C#

    更多研究带来这个:Build your own netstat.exe with c#。这使用 P/Invoke 调用 GetExtendedTcpTable 并使用与 netstat 相同的结构。

    【讨论】:

    【解决方案2】:

    在此处查看 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。

    【讨论】:

    • 找不到页面 :(
    猜你喜欢
    • 2016-04-10
    • 2018-12-27
    • 2015-08-25
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2015-10-27
    • 2013-11-07
    相关资源
    最近更新 更多