I need to query the Hardware-Id for a network adapter using C#.
Using the System.Management i can query the details of deviceID,description etc. but not the hardware id.
where, listBox1 is a simple listbox control instance for showing the items on a winform app.
For ex:
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());
}
However looking at MSDN WMI reference there is no way i can get the HardwareId. By using the devcon tool (devcon hwids =net) however i know that each device is associated with a HardwareId
Any help is deeply appreciated
The HardwareID you're looking for is located in another WMI Class. Once you have an instance of a Win32_NetworkAdapeter, you can select a Win32_PnpEntry using the PNPDeviceId. Here is a sample code that list all network adapters and their hardware id, if any:
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]);
}
}
}