user5883013 February 2016

How to read WMI data of a USB (PnP) device, in this case the hardware ids, in C#?

How can I get the hardware id from an USB device? I know how to get that id with device-manager, but I want to get it via C#. Is that possible? This is what I´ve done already, but that delivers me not the hardware if which consists of the vendor id (VID) and the product id (PID):

ManagementObjectSearcher USB = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
ManagementObjectCollection USBinfo = USB.Get();

foreach (ManagementObject MO in USBinfo)
{
    serialNumber = (string)MO["DeviceID"];
    name = (string)MO["Name"];
    Drives.Add(new KeyValuePair<String, String>(name, serialNumber));
}

I also tried it with "PNPDeviceID" and "SerialNumber" instead of "DeviceID", but that also doesn´t replys the hardware-id that I need.

Answers


Kutty Rajesh Valangai February 2016

In Console application, add refrence-->.Net-->system.Management-->add

namespace ConsoleApplication1
{
  using System;
  using System.Collections.Generic;
  using System.Management; // need to add System.Management to your project references.

  class Program
  {
    static void Main(string[] args)
    {
      var usbDevices = GetUSBDevices();

      foreach (var usbDevice in usbDevices)
      {
        Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
            usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
      }

      Console.Read();
    }

    static List<USBDeviceInfo> GetUSBDevices()
    {
      List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
      ManagementObjectCollection collection;

      using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
        collection = searcher.Get();      

      foreach (var device in collection)
      {
        devices.Add(new USBDeviceInfo(
        (string)device.GetPropertyValue("DeviceID"),
        (string)device.GetPropertyValue("PNPDeviceID"),
        (string)device.GetPropertyValue("Description")
        ));
      }

      collection.Dispose();
      return devices;
    }
  }

  class USBDeviceInfo
  {
    public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
    {
      this.DeviceID = deviceID;
      this.PnpDeviceID = pnpDeviceID;
      this.Description = description;
    }

    public string DeviceID { get; private set; }
    public string PnpDeviceID { get; private set; }
    public string Description { get; private set; }
  }
}


Markus Safar February 2016

Original answer (see updated answer below)

You need to take a look at the Win32_PnPEntity WMI class. According to the mentioned description you can see, that you just need to check for HardwareID.

The following code show's that in a "quick and dirty" way, I would suggest creating a Win32_PnpEntity class which exposes the data via properties.

ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_PnPEntity");
ManagementObjectCollection deviceCollection = searcher.Get();

foreach (var device in deviceCollection)
{
    try
    {
        string caption = (string)device.GetPropertyValue("Caption");

        if (caption == null)
            continue;

        Console.WriteLine(caption);

        string[] hardwareIDs = (string[])device.GetPropertyValue("HardwareID");

        if (hardwareIDs == null)
            continue;

        foreach (string hardwareID in hardwareIDs)
        {
            Console.WriteLine(hardwareID);
        }

        Console.WriteLine();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

Updated answer

As the generic question is How to read WMI data of USB (PnP) device in C#, in this case the hardware ids?, I've written a Win32_PnpEntity class (stripped of comments as the post is limited to 30000 characters? If anyone want's the commented version, drop me a line) which takes a ManageementBasObject and provides all data of that given entity as properties.

Usage:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_PnPEntity");
ManagementObjectCollection deviceCollection = searcher.Get();

foreach (var entity in deviceCollection)
{
    Win32_PnPEntity device  

Post Status

Asked in February 2016
Viewed 1,563 times
Voted 8
Answered 2 times

Search




Leave an answer