簡體   English   中英

獲取全稱.network interface C#

[英]Get full name of network interface C#

我試圖從 ipconfig /all 中找到 .network 接口名稱。 例如,如果 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 

我想打印“Ethe.net 適配器 Npcap 環回適配器”。 這是我試過的:

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

但它只打印“Ethe.net”而不是完整的東西。

查看 ipconfig 的 output,我認為您看到的文本由兩部分組成:

  1. 轉換為標准字符串的NetworkInterfaceType屬性
  2. Name屬性。

所以你真的只需要一個 function 來將枚舉轉換成一個字符串,例如它可以像這樣簡單:

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}";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM