I want a list of connected USB Device in C#.NET. but I can't understand what to do.
Asked
Active
Viewed 1.0k times
0
-
1have you even tried to search for a solution? what about: http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices just type your question into google... – bema May 06 '13 at 09:53
-
I need to show it in a list box. I've seen your link before. but some code do not work. Error 1 The name 'GetUSBDevices' does not exist in the current context G:\USB_Device_List\USB_Device\USB_Device\Form1.cs 25 34 USB_Device – Maruf Hossain May 06 '13 at 10:08
-
@MarufHossain: the GetUSBDevices() is defined just below the Main function. – Jean Hominal May 06 '13 at 10:20
1 Answers
3
Add a reference to System.Management in project,After that refer code
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}", usbDevice.DeviceID);
}
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID)
{
this.DeviceID = deviceID;
}
public string DeviceID { get; private set; }
}
}
Hope it helps you.

Neeraj Dubey
- 4,401
- 8
- 30
- 49