简体   繁体   English

获取全称.network interface C#

[英]Get full name of network interface C#

I'm trying to find the.network interface name from ipconfig /all.我试图从 ipconfig /all 中找到 .network 接口名称。 So for example if the ipconfig /all output is:例如,如果 ipconfig /all output 是:

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 

I want to print "Ethe.net adapter Npcap Loopback Adapter".我想打印“Ethe.net 适配器 Npcap 环回适配器”。 This is what I've tried:这是我试过的:

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

But it only prints "Ethe.net" instead of the full thing.但它只打印“Ethe.net”而不是完整的东西。

Looking at the output of ipconfig, I think the text you are seeing is made up of two things:查看 ipconfig 的 output,我认为您看到的文本由两部分组成:

  1. The NetworkInterfaceType property converted into standard strings转换为标准字符串的NetworkInterfaceType属性
  2. The Name property. Name属性。

So you really just need a function to convert the enum into a string, for example it could be as simple as this:所以你真的只需要一个 function 来将枚举转换成一个字符串,例如它可以像这样简单:

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

And construct the name like this:并像这样构建名称:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM