【问题标题】:Get full name of network interface C#获取网络接口的全名C#
【发布时间】:2021-01-27 17:18:46
【问题描述】:

我正在尝试从 ipconfig /all 中查找网络接口名称。例如,如果 ipconfig /all 输出是:

Ethernet adapter Npcap Loopback Adapter:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Npcap Loopback Adapter
   Physical Address. . . . . . . . . : 01-00-4C-5F-5F-50
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Autoconfiguration IPv4 Address. . : 169.254.183.10(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :
   NetBIOS over Tcpip. . . . . . . . : Enabled 

我想打印“以太网适配器 Npcap 环回适配器”。这是我尝试过的:

NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in interfaces)
{
     richTextBox1.AppendText(adapter.Name);
}

但它只打印“以太网”而不是完整的东西。

【问题讨论】:

标签: c# networking command-line


【解决方案1】:

查看 ipconfig 的输出,我认为您看到的文本由两部分组成:

  1. NetworkInterfaceType 属性转换为标准字符串
  2. Name 属性。

所以你真的只需要一个函数来将枚举转换为字符串,例如它可以像这样简单:

private string GetNetworkTypeName(NetworkInterfaceType type) =>
    type switch
    {
        NetworkInterfaceType.Ethernet => "Ethernet adapter",
        NetworkInterfaceType.Wireless80211 => "Wireless LAN adapter",
        //etc etc...
        _ => "Other"
    };

并像这样构造名称:

var interfaceName = $"{GetNetworkTypeName(adapter.NetworkInterfaceType)} {adapter.Name}";

【讨论】:

  • 这对我不起作用,因为我的计算机有多个以太网连接。我也需要这个才能在任何计算机上工作。
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多