Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to detect my Android phone on USB port connect to my PC ?
Thank you

What I have tried:

C#
public MainWindow()
{
  InitializeComponent();

  var usbDevices = GetUSBDevices();

  foreach (var usbDevice in usbDevices)
  {
    MessageBox.Show(usbDevice.DeviceID + "  " + usbDevice.PnpDeviceID+ "  " + usbDevice.Description);
  }
}

static List<usbdeviceinfo> GetUSBDevices()
{
  List<usbdeviceinfo> devices = new List<usbdeviceinfo>();

  ManagementObjectCollection collection;
  using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity"))
                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; }
}
Posted
Updated 5-Jun-17 5:41am
v2

1 solution

Excellent answer (number 2) on this site;

How to Search for USB device events[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900