【问题标题】:How to get nic card name如何获取网卡名称
【发布时间】:2012-06-18 20:47:46
【问题描述】:

任何人都知道如何获取网卡名称

当我做 ipconfig/all 我可以得到这个

Ethernet adapter XC99HT:

   Connection-specific DNS Suffix  . : xx.xx.com
   Description . . . . . . . . . . . : HP NC382i DP Multifunction Gigabit Server
 Adapter
   Physical Address. . . . . . . . . : F4-CE-46-94-E8-B0
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 177.77.153.48(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 177.77.153.1
   DNS Servers . . . . . . . . . . . : 177.77.124.129
                                       177.77.124.130
   Primary WINS Server . . . . . . . : 177.77.124.129
   Secondary WINS Server . . . . . . : 177.77.124.130
   NetBIOS over Tcpip. . . . . . . . : Enabled

想要获得“HP NC382i DP 多功能千兆服务器 适配器”只需使用/传递以太网名称“XC99HT”

【问题讨论】:

  • 您总是可以执行 ipconfig 命令,然后使用正则表达式读取输出,但我不打算为您编写代码 ;-)(我希望有人给您比这更好的答案)

标签: c# windows-server-2008-r2 ethernet nic


【解决方案1】:

类似this

public static void ShowInterfaceSummary()

{

NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in interfaces)
{                
    Console.WriteLine ("Name: {0}", adapter.Name);
    Console.WriteLine(adapter.Description);
    Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
    Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
    Console.WriteLine("  Operational status ...................... : {0}", 
        adapter.OperationalStatus);
    string versions ="";

    // Create a display string for the supported IP versions.
    if (adapter.Supports(NetworkInterfaceComponent.IPv4))
    {
         versions = "IPv4";
     }
    if (adapter.Supports(NetworkInterfaceComponent.IPv6))
    {
        if (versions.Length > 0)
        {
            versions += " ";
         }
        versions += "IPv6";
    }
    Console.WriteLine("  IP version .............................. : {0}", versions);
    Console.WriteLine();
}
Console.WriteLine();

}

【讨论】:

    【解决方案2】:

    要在代码中正确执行此操作,您可能需要使用 WMI。 WMI(Windows Management Intrumentation)是 Windows 中的一个“元数据库”,其中包含有关设备级别发生的所有事情的信息。您可以使用 .NET 中的 System.Management 命名空间访问它,主要是 ManagementObjectSearcher 类。您使用类似于 SQL 的语法搜索 WMI。这是一个返回所有活动网络适配器的基本查询:

    select * from Win32_NetworkAdapterConfiguration where IPEnabled = true
    

    您使用 IPConfig(以及更多)可以看到的几乎所有内容都可以从生成的 ManagementObjectCollection 中获得。不幸的是,我不知道对象的哪个字段会有数据“XC99HT”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 2015-03-28
      • 2022-08-17
      相关资源
      最近更新 更多