首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取网络接口C#的全名

获取网络接口C#的全名
EN

Stack Overflow用户
提问于 2021-01-28 01:18:46
回答 1查看 64关注 0票数 0

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

代码语言:javascript
复制
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 

我想打印"Ethernet adapter Npcap Loopback Adapter“。这是我尝试过的:

代码语言:javascript
复制
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in interfaces)
{
     richTextBox1.AppendText(adapter.Name);
}

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

EN

回答 1

Stack Overflow用户

发布于 2021-01-28 01:35:44

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

因此,您实际上只需要一个函数来将枚举转换为字符串,例如,它可以像这样简单:

代码语言:javascript
复制
private string GetNetworkTypeName(NetworkInterfaceType type) =>
    type switch
    {
        NetworkInterfaceType.Ethernet => "Ethernet adapter",
        NetworkInterfaceType.Wireless80211 => "Wireless LAN adapter",
        //etc etc...
        _ => "Other"
    };

并像这样构造名称:

代码语言:javascript
复制
var interfaceName = $"{GetNetworkTypeName(adapter.NetworkInterfaceType)} {adapter.Name}";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65924121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档