3

我需要使用 C# 查询网络适配器的硬件 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 相关联

任何帮助都深表感谢

4

1 回答 1

3

您要查找的 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]);
                }
            }
        }
于 2011-09-20T06:20:02.000 回答