【问题标题】:how to get hardware-id for a network adapter programmatically in C#如何在 C# 中以编程方式获取网络适配器的硬件 ID
【发布时间】:2011-09-20 05:31:28
【问题描述】:

我需要使用 C# 查询网络适配器的 Hardware-Id。

使用 System.Management 我可以查询设备 ID、描述等的详细信息,但不能查询硬件 ID。

其中,listBox1 是一个简单的列表框控件实例,用于在 winform 应用程序上显示项目。

例如:

ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_NetworkAdapter");
                mbsList = mbs.Get();
                foreach (ManagementObject mo in mbsList)
                {
                    listBox1.Items.Add("Name : " + mo["Name"].ToString());
                    listBox1.Items.Add("DeviceID : " + mo["DeviceID"].ToString());
                    listBox1.Items.Add("Description : " + mo["Description"].ToString());
                }

但是查看MSDN WMI 参考,我无法获得 HardwareId。 通过使用 devcon 工具 (devcon hwids =net) 但是我知道每个设备都与一个 HardwareId 相关联

非常感谢任何帮助

【问题讨论】:

  • 没有。我需要 HardwareId 作为网络适配器。而不是关于处理器的任何细节。

标签: c# wmi-query


【解决方案1】:

您要查找的 HardwareID 位于另一个 WMI 类中。一旦有了 Win32_NetworkAdapeter 的实例,就可以使用 PNPDeviceId 选择 Win32_PnpEntry。以下是列出所有网络适配器及其硬件 ID(如果有)的示例代码:

        ManagementObjectSearcher adapterSearch = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapter");

        foreach (ManagementObject networkAdapter in adapterSearch.Get())
        {
            string pnpDeviceId = (string)networkAdapter["PNPDeviceID"];
            Console.WriteLine("Description  : {0}", networkAdapter["Description"]);
            Console.WriteLine(" PNPDeviceID : {0}", pnpDeviceId);

            if (string.IsNullOrEmpty(pnpDeviceId))
                continue;

            // make sure you escape the device string
            string txt = "SELECT * FROM win32_PNPEntity where DeviceID='" + pnpDeviceId.Replace("\\", "\\\\") + "'";
            ManagementObjectSearcher deviceSearch = new ManagementObjectSearcher("root\\CIMV2", txt);
            foreach (ManagementObject device in deviceSearch.Get())
            {
                string[] hardwareIds = (string[])device["HardWareID"];
                if ((hardwareIds != null) && (hardwareIds.Length > 0))
                {
                    Console.WriteLine(" HardWareID: {0}", hardwareIds[0]);
                }
            }
        }

【讨论】:

  • Simon 我收到无效查询异常。
  • 我的环境:XP Embedded SP-2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
相关资源
最近更新 更多