Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

Please tell me how to load all the USB hub names in a combo box using c#.
for serial port i used
C#
SerialPort.GetPortNames()
, but for USB how to do the same.

Thank you
Posted
Updated 13-Jul-14 21:00pm
v2

I think you can't get a list of usb ports in the pc,just the lists of usb DEVICES connected. Refer http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices[^
 
Share this answer
 
Comments
[no name] 14-Jul-14 5:23am    
It's not seems to be working for me.
i am getting value like below in the combo box
"USB\ROOT_HUB20\4&280E9CA0&0"
Pikoh 14-Jul-14 5:33am    
You can do this using a WMI query on Win32_USBHub.

http://msdn.microsoft.com/en-us/library/aa394506(v=vs.85).aspx[^]

Download the code from here:
http://code.msdn.microsoft.com/windowsdesktop/Using-WMI-with-C-4e5a9ee1[^]

Change the line:

C#
SelectQuery query = new SelectQuery("select * from Win32_Service");

to:

C#
SelectQuery query = new SelectQuery("select * from Win32_USBHub");

AND change:
C#
List<string> serviceNames =
    (from ManagementObject service in services select service["Caption"].ToString()).ToList();

lstServices.DataSource = serviceNames;


to:
C#
List<string> USBNames =
    (from ManagementObject service in services select service["Name"].ToString()).ToList();

lstServices.DataSource = USBNames;
 
Share this answer
 
v2

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