简体   繁体   中英

Getting the model name of a connected USB device in .NET

In my WinForms application, I want to get a list of connected USB objects and then obtain the model property of those connected USB devices, as I need to be able to tell the user when they have connected a certain USB device or not.

Can anyone share an approach, via WMI or something else perhaps, to both get a list of connected devices and get the model property of those devices? I've adapted some code from this question on using WMI to get device information. The code below works fine but the parameters that are obtained ("DeviceID", "PNPDeviceID" etc.) aren't useful at all when it comes to identifying the devices in human understandable terms and I don't know how to get the Model property that is shown in Devices and Printers as seen here .

public static List<USBDeviceInfo> GetUSBDevices(){

    List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

    ManagementObjectCollection collection;

    using (var searcher = new ManagementObjectSearcher(@"Select * From 
              Win32_USBControllerDevice")) 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; 
}

using the class

public 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; }
    }
}

I wanted to make this post in case I'm missing a simpler solution to my specific problem. I'm also currently trying to reverse engineer USB View ala this question since USB view is getting the information I need somehow.

For the record I am using C# in Visual Studio 2015.

I was able to implement the top level functionality I needed via a work around; I identified a part of the DeviceID string that was returned that was unique to the brand of RFID scanners that I need for my application.

I parsed the returned deviceID string and checked to see if that unique value was present, and updated my application accordingly to reflect that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM