c# - Get List of connected USB Devices -


how can list of connected usb devices on windows computer?

add reference system.management project, try this:

namespace consoleapplication1 {   using system;   using system.collections.generic;   using system.management; // need add system.management 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 * 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; }   } } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -