用c#写一个端口占用情况
时间: 2023-07-13 14:30:30 浏览: 39
可以使用C#编写一个程序来获取当前计算机上所有正在使用的端口信息,代码如下:
```csharp
using System;
using System.Net;
using System.Net.NetworkInformation;
namespace PortUsageChecker
{
class Program
{
static void Main(string[] args)
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners();
IPEndPoint[] udpEndPoints = properties.GetActiveUdpListeners();
Console.WriteLine("TCP Ports:");
foreach (IPEndPoint endPoint in tcpEndPoints)
{
Console.WriteLine(endPoint.Port);
}
Console.WriteLine("UDP Ports:");
foreach (IPEndPoint endPoint in udpEndPoints)
{
Console.WriteLine(endPoint.Port);
}
Console.ReadKey();
}
}
}
```
这个程序使用了.NET框架的`IPGlobalProperties`类,可以获取当前计算机上所有正在使用的TCP端口和UDP端口的信息。运行程序后,控制台将输出当前计算机上所有正在使用的TCP端口和UDP端口。