如何在C#中以编程方式获取网络适配器的硬件ID
作者:互联网
我需要使用C#查询网络适配器的硬件ID.
使用System.Management,我可以查询deviceID,描述等的详细信息,但不能查询硬件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位于另一个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]);
}
}
}
标签:wmi-query,c 来源: https://codeday.me/bug/20191207/2087517.html