编写一个控制台应用程序,查询并显示本机运行的所有因特网协议的名称及相的协议号。
时间: 2024-10-13 21:11:39 浏览: 59
在控制台应用程序中查询和显示本地运行的所有互联网协议(IP)及其对应的协议号通常涉及到网络编程和系统信息获取。这里以Windows操作系统为例,你可以使用`System.Net.NetworkInformation`命名空间中的类来完成这个任务,特别是`IPInterfaceProperties`和`Protocol`枚举。
以下是一个简单的C#代码示例:
```csharp
using System;
using System.Net.NetworkInformation;
namespace InternetProtocolsFinder
{
class Program
{
static void Main(string[] args)
{
// 获取所有网络接口
IPInterfaceProperties[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (IPInterfaceProperties interfaceProp in interfaces)
{
Console.WriteLine($"接口名称: {interfaceProp.Name}");
// 遍历接口的协议列表
foreach (IPProtocol protocol in interfaceProp.ProtocolNames)
{
int protocolNumber = Convert.ToInt32(Enum.Parse(typeof(IPProtocol), protocol));
Console.WriteLine($"协议名称: {protocol}, 协议号: {protocolNumber}");
}
}
Console.ReadKey();
}
}
}
```
当你运行这个程序,它会列出所有可用的网络接口以及它们关联的IP协议(如TCP、UDP等)及其对应的协议号。
阅读全文
相关推荐



















