8

Possible Duplicate:
Get List Of USB Devices

Im making a WPF app.

Im looking for a way to list all plugged in USB devices (disks!) in my comboBox.

I can list all drives using DriveInfo.GetDrives(), but is there a simple way to filter that to USB devices?

thanx

Community
  • 1
  • 1
no9
  • 6,424
  • 25
  • 76
  • 115
  • 1
    possible duplicate of [Get List Of USB Devices](http://stackoverflow.com/questions/3331043/get-list-of-usb-devices) and [different between card reader to usb using c#](http://stackoverflow.com/questions/3329745/different-between-card-reader-to-usb-using-c/3329822#3329822) – H H Jul 27 '10 at 12:19

2 Answers2

18
 foreach (DriveInfo drive in DriveInfo.GetDrives())
 {
     if (drive.DriveType == DriveType.Removable)
     {
      ..
     }
 }
Ragunathan
  • 2,007
  • 1
  • 14
  • 10
  • Be careful with this that you don't (occasionally, never with a debugger attached) get a horrible messagebox from somewhere deep in Windows asking you to insert a disk into a drive. – Will Dean Jul 27 '10 at 12:38
  • i have additional question. My combobox had binding to property (type List) on my viewmodel. It works fine, but if i plug in USB disk i have to reload the window in order to repopulate combobox. Is there a way that i could repopulate combobox once the USB devide is plugged in/ out ? – no9 Jul 28 '10 at 05:50
  • also i shrinked the code u gave me to: return DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable).ToList(); – no9 Jul 28 '10 at 07:14
  • Use ObservableCollection in viewmodel to bind to the Combobox. Create a DispatcherTimer with some interval to check for Drives, if any new drives found add that to the observablecollection; this is will displayed in the UI. – Ragunathan Jul 28 '10 at 07:19
  • 1
    This does not work with some external hard drives. At the moment the only way to detect if it is a hard disk connected via USB is to use some ugly WMI stuff. You can see a code sample at http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices – seveves Aug 14 '15 at 11:26
  • Isn't this return all the removable drives? including SATA which is not a USB? – AaA Mar 24 '18 at 13:38
0

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9f9eb8f5-297f-4acd-a9af-aafbe384fd71/

The accepted answer at that link seems to do what you're asking.

Laplace
  • 491
  • 1
  • 3
  • 9
  • thanx, ive been looking into that solution before, but it seemed too complicated ... – no9 Jul 28 '10 at 05:52