如何在C#中确定Windows进程使用的tcp端口 [英] How to determine tcp port used by Windows process in C#

查看:36
本文介绍了如何在C#中确定Windows进程使用的tcp端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a managed class/method that would provide the TCP port number(s) used by a particular Windows processes?

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

I'm really looking for a .NET equivalent of the following CMD line:

netstat -ano |find /i "listening"

推荐答案

除了PID,看看这个:

Except for PID, take a look this:

IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners();
TcpConnectionInformation[] tcpConnections = 
    ipProperties.GetActiveTcpConnections();

foreach (TcpConnectionInformation info in tcpConnections)
{
    Console.WriteLine("Local: {0}:{1}
Remote: {2}:{3}
State: {4}
", 
        info.LocalEndPoint.Address, info.LocalEndPoint.Port,
        info.RemoteEndPoint.Address, info.RemoteEndPoint.Port,
        info.State.ToString());
}
Console.ReadLine();

来源:C# 中的 Netstat

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

A bit more research bring this: Build your own netstat.exe with c#. This uses P/Invoke to call GetExtendedTcpTable and using same structure as netstat.

这篇关于如何在C#中确定Windows进程使用的tcp端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆