4

I am trying to get the names of connected usb devices, like mobile phones or usb sticks.

Exmaple of usb-device

As I browsed stackoverflow before I got these approach, but I cant find the right property.

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"),
            (string)device.GetPropertyValue("Name"),
            (string)device.GetPropertyValue("Caption")
            ));
        }

        collection.Dispose();
        return devices;
}

Class USBDeviceInfo:

class USBDeviceInfo
{
    public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name, string caption)
    {
        this.DeviceID = deviceID;
        this.PnpDeviceID = pnpDeviceID;
        this.Description = description;
        this.Name = name;
        this.Caption = caption;
    }
    public string DeviceID { get; private set; }
    public string PnpDeviceID { get; private set; }
    public string Description { get; private set; }
    public string Name { get; private set; }
    public string Caption { get; private set; }

}

I would greatly appreciate some help

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
cKai
  • 57
  • 10
  • "but I cant find the right property." what do you mean by? – habib Aug 16 '19 at 15:20
  • i like to get the name of that mobile phone "kai" as example. That name property only gives me "USB-Root-Hub" or "Generic USB Hub" etc. – cKai Aug 16 '19 at 15:23
  • 1
    you might want to use a different library than wmi for that information – Daniel A. White Aug 16 '19 at 15:26
  • @DanielA.White do you have any advice which library suits? – cKai Aug 16 '19 at 15:29
  • Different USB devices use different properties. So it may not be the same for different devices. Some device do not even have Name property. – jdweng Aug 16 '19 at 15:49
  • I use HIDSharp which makes that sort of thing much easier. The HIDDevice class has a "getFriendlyName()" method which is probably what you're looking for. – Duston Aug 16 '19 at 16:17

1 Answers1

4

Instead of querying Win32_USBHub, you could try Win32_PnPEntity. This returns all plug and play devices, so I added a filter to remove any whose device id doesn't start with "USB". There's probably a better way, but I used in the past so though I'd share here.

Here's a modified version of your code:

class USBDeviceInfo
{
    public USBDeviceInfo(string deviceId, string name, string description)
    {
        DeviceId = deviceId;
        Name = name;
        Description = description;
    }

    public string DeviceId { get; }
    public string Name { get; }
    public string Description { get; }

    public override string ToString()
    {
        return Name;
    }
}

public class Program
{
    static List<USBDeviceInfo> GetUSBDevices()
    {
        var devices = new List<USBDeviceInfo>();

        using (var mos = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity"))
        {
            using (ManagementObjectCollection collection = mos.Get())
            {
                foreach (var device in collection)
                {
                    var id = device.GetPropertyValue("DeviceId").ToString();

                    if (!id.StartsWith("USB", StringComparison.OrdinalIgnoreCase)) 
                        continue;

                    var name = device.GetPropertyValue("Name").ToString();
                    var description = device.GetPropertyValue("Description").ToString();
                    devices.Add(new USBDeviceInfo(id, name, description));
                }
            }
        }

        return devices;
    }

    private static void Main()
    {
        GetUSBDevices().ForEach(Console.WriteLine);

        Console.ReadKey();
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43