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

问题描述

有一个托管类/将提供的TCP端口号(S)由特定的Windows进程的方法?

我真的找了.NET相当于下列CMD行:

 的netstat -ano |查找/我听


解决方案

除了PID,看看这样的:

  IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();IPEndPoint [] =终点ipProperties.GetActiveTcpListeners();
TcpConnectionInformation [] = tcpConnections
    ipProperties.GetActiveTcpConnections();的foreach(在tcpConnections TcpConnectionInformation信息)
{
    Console.WriteLine(本地:{0}:{1} \\ n远程:{2}:{3} \\ NSTATE:{4} \\ n
        info.LocalEndPoint.Address,info.LocalEndPoint.Port,
        info.RemoteEndPoint.Address,info.RemoteEndPoint.Port,
        info.State.ToString());
}
到Console.ReadLine();

来源:

多一点的研究把这个:构建自己的netstat.exe用C#。这里使用的P / Invoke调用 GetExtendedTcpTable ,并使用相同的结构,的netstat

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

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

netstat -ano |find /i "listening"
解决方案

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}\nRemote: {2}:{3}\nState: {4}\n", 
        info.LocalEndPoint.Address, info.LocalEndPoint.Port,
        info.RemoteEndPoint.Address, info.RemoteEndPoint.Port,
        info.State.ToString());
}
Console.ReadLine();

Source: Netstat in C#

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端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:52