How can I get a list of all the connected USB devices on a windows computer?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Adel Hazzah's answer gives working code, Daniel Widdis's and Nedko's comments mention that you need to query Win32_USBControllerDevice and use its Dependent property, and Daniel's answer gives a lot of detail without code.
Here's a synthesis of the above discussion to provide working code that lists the directly accessible PNP device properties of all connected USB devices:
You'll need to add exception handling if you want it. Consult Daniel's answer if you want to figure out the device tree and such.
If you change the ManagementObjectSearcher to the following:
So the "GetUSBDevices() looks like this"
}
Your results will be limited to USB devices (as opposed to all types on your system)
This is a much simpler example for people only looking for removable usb drives.
using System.IO;
Add a reference to System.Management for your project, then try something like this:
To see the devices I was interested in, I had replace
Win32_USBHub
byWin32_PnPEntity
in Adel Hazzah's code, based on this post. This works for me: