Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find usb devices in c#?

I need to iterate through the ports connected to the computer, and find a specific device. Please have a look at the below image:

enter image description here

You can see there are 4 device names with the same vendor and product id. but, i need to find the the port of the first one, which is highlighted in blue colour. it seems like the only difference they have is friend name (the description is friend name).

What is the easiest way of achieving this in C#.net? I have done this in 'qt' and I need to know how this can be done in C#, .net framework 4, using vs 2010 professional. I have gone through questions like this but as you can see, they are no help to my situation.

like image 343
PeakGen Avatar asked Aug 13 '13 06:08

PeakGen


People also ask

How do I find my USB on my computer?

On Windows 8 or 10, right-click the Start button and select “Device Manager”. On Windows 7, press Windows+R, type devmgmt. msc into the Run dialog, and press Enter. Expand the “Disk Drives” and “USB Serial Bus controllers” sections and look for any devices with a yellow exclamation mark on their icon.

How do I find an unknown USB device?

Locate any hardware devices that are reported to be unrecognized (may be indicated by a yellow ! icon, or something similar), right-click the device, and select Properties. Select the Details tab. View the plug-and-play Hardware ID information in the Property selection list.

How do I see USB properties?

Right-click the USB flash drive icon and select Properties on the menu. Ensure the General tab of the Properties dialog is visible. The number of bytes next to Free space is the amount of free storage on your USB drive. The total size of the drive is next to Capacity.


1 Answers

If you use libusbdotnet you should be able to do something like this:

public static void RetrieveUSBDevices(int vid, int pid)
{
        var usbFinder = new UsbDeviceFinder(vid, pid);
        var usbDevices = new UsbRegDeviceList();
        usbDevices = usbDevices.FindAll(usbFinder);
}

You should then be able to iterate the usbDevices and check for the correct FullName. Haven´t tested this though so it´s theoretical.

UPDATE: Tried that and it worked fine - what is the problem? Why vote down due to your own incompetence?

This would also work:

private static void Method()
{
var list = GetMyUSBDevices();
//Iterate list here and use Description to find exact device
}


private static List<UsbDevice> GetMyUSBDevices()
{
    var vid = 32903;
    var pid = 36;

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

    var usbDevice = 
        (from ManagementBaseObject device in collection 
        select new UsbDevice(
        (string) device.GetPropertyValue("DeviceID"), 
        (string) device.GetPropertyValue("Description"))).ToList();

    var devices = new List<UsbDevice>();

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

    collection.Dispose();

    return (from d in devices where d.DeviceId.Contains("VID_") && d.DeviceId.Contains("PID_") && d.PID.Equals(pid) && d.VID.Equals(vid) select d).ToList();
}

public class UsbDevice
{
    public UsbDevice(string deviceId, string description)
    {
        DeviceId = deviceId;
        Description = description;
    }

    public string DeviceId { get; private set; }
    public string Description { get; private set; }

    public int VID 
    {
        get { return int.Parse(GetIdentifierPart("VID_"), System.Globalization.NumberStyles.HexNumber); }
    }

    public int PID
    {
        get { return int.Parse(GetIdentifierPart("PID_"), System.Globalization.NumberStyles.HexNumber); }
    }

    private string GetIdentifierPart(string identifier)
    {
        var vidIndex = DeviceId.IndexOf(identifier, StringComparison.Ordinal);
        var startingAtVid = DeviceId.Substring(vidIndex + 4);
        return startingAtVid.Substring(0, 4);
    }
}
like image 55
Marcus Avatar answered Nov 14 '22 23:11

Marcus