Click here to Skip to main content
13,590,119 members
Click here to Skip to main content
Add your own
alternative version


130 bookmarked
Posted 7 Mar 2010
Licenced Ms-PL

Enumerate and Auto-Detect USB Drives

, 9 Mar 2010
Rate this:
Please Sign up or sign in to vote.
This article describes how to use the .NET System.Management WMI (Windows Management Instrumentation) wrappers to enumerate and describe USB disk drives. It also includes a non-Interop solution for detecting drive state changes as they come online or go offline.


This article describes how to use the .NET System.Management WMI (Windows Management Instrumentation) wrappers to enumerate and describe USB disk drives. It also includes a non-Interop solution for detecting drive state changes as they come online or go offline.



While writing iTuner, I needed to be able to enumerate USB disk drives (MP3 players) and allow the user to select one with which iTuner could synchronize iTunes playlists. In order to select the correct device, the user needs some basic identifying information beyond just drive letter such as volume name, manufacturer's model name, and available disk space. As an added feature, I wanted the UI to update automatically as USB drives come online or go offline. Finally, I wanted to accomplish this through a tidy, simple interface that did not add tremendous complexity to the main application.

Caveat: The UsbManager.GetAvailableDisks method can be run in any application type. However, the StateChanged event will not be fired unless running in the context of a WPF or Windows Forms application. This is because the event handler relies on Windows message processing to intercept these state changes as explained below.

Using the Code

As an application developer and consumer of this API, you first need an instance of the UsbManager class. This class exposes a very simple interface:

class UsbManager : IDisposable
    public UsbManager ();
    public event UsbStateChangedEventHandler StateChanged;
    public UsbDiskCollection GetAvailableDisks ();

class UsbDiskCollection : ObservableCollection<UsbDisk>
    public bool Contains (string name);
    public bool Remove (string name);

class UsbDisk
    public ulong FreeSpace { get; }
    public string Model { get; }
    public string Name { get; }
    public ulong Size { get; }
    public string Volume { get; }
    public string ToString ();
  1. Instantiate a new UsbManager using its parameterless constructor.
  2. Wire up an event handler to the StateChanged event.
  3. If desired, call GetAvailableDisks to retrieve a list of current USB disks.

The UsbDisk class abstracts the information pertaining to a particular USB disk. It includes only the most recognizable fields that a typical end user might use to differentiate drives such as the drive letter (Name), volume name, and manufacturer's model name. It also specifies the available free space and total disk size, both specified in bytes. While other information such as serial numbers, partition or sector data might be of interest to developers, they're quite esoteric to end users.

That's it! Happy coding! OK, keep reading if you want to understand how it's all put together.

Inside the Code

Developing this API was first and foremost an exercise in discovering the WMI classes and their relationships. Unfortunately, WMI does not have a single WMI_USBDiskDrive class with all the properties we want. But the information is there. I started by using the WMI Explorer utility from KS-Soft. It's free and available for download on their Web site.

A Walk Around the WMI Classes

The first WMI class that draws your attention is Win32_DiskDrive. Drives are listed with creative and obvious names like "\\PHYSICALDRIVE0". We can filter these by looking at only those with an InterfaceType property value of "USB". Win32_DiskDrive also specifies the Model and Size of the drive. There are lots of other properties, but none are very interesting in this case. Here's the WMI query that retrieves the DeviceID and Model name for USB drives:

select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'

My next stop was the Win32_LogicalDisk class. This gets a bit more interesting right off the bat because instances are listed as drive letters like "C:", "D:", and "S:". We can also fetch the FreeSpace and VolumeName properties. Here's the WMI query:

select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='S:'

We now need a way to associate Win32_DiskDrive and Win32_LogicalDisk so we can marry these bits of information together. You might think there would be some shared field that allows you join the two classes. No such luck. And that's exactly where the Web came to the rescue and I discovered a bit of code tucked away on MSDN that demonstrates how to associate these classes. We can use the associators operator to discover associations between various classes. Given a Win32_DiskDrive instance, we can use its DeviceID property to determine the Win32_DiskPartition instance associated via Win32_DiskDriveToDiskPartition:

associators of {Win32_DiskDrive.DeviceID='\\PHYSICALDRIVE1'}
      where AssocClass = Win32_DiskDriveToDiskPartition

Then using Win32_DiskPartition.DeviceID, we can determine the Win32_LogicalDisk instance associated via Win32_LogicalDiskToPartition:

associators of {Win32_DiskPartition.DeviceID='Disk #0, Partition #1'}
      where AssocClass = Win32_LogicalDiskToPartition

Implementing the WMI Queries Using System.Management

To execute a WMI query, we can use the System.Management.ManagementObjectSearcher class. This class always has the same pattern: search, get, enumerate as shown here:

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher("select * from Win32_DiskDrive");

ManagementObjectCollection items = searcher.Get();

foreach (ManagementObject item in items)

Given the cascading calls needed to query the four WMI classes, we would end up with a fairly ugly nesting of foreach loops. In order to clean this up and make the logic more obvious, I created a simple extension method for the ManagementObjectSearcher class. This extension adds a First() method to the ManagementObjectSearcher class that invokes its Get method, enumerates the resultant collection and immediately returns the first item in that collection:

public static ManagementObject First (this ManagementObjectSearcher searcher)
    ManagementObject result = null;
    foreach (ManagementObject item in searcher.Get())
        result = item;
    return result;

Combine this helper extension with the WMI queries above and we end up with a straightforward code in UsbManager.GetAvailableDisks(). Yes, we still have a nested structure, but testing for null is much more clear than the alternative!

public UsbDiskCollection GetAvailableDisks ()
    UsbDiskCollection disks = new UsbDiskCollection();

    // browse all USB WMI physical disks
    foreach (ManagementObject drive in
        new ManagementObjectSearcher(
            "select DeviceID, Model from Win32_DiskDrive " +
             "where InterfaceType='USB'").Get())
        // associate physical disks with partitions
        ManagementObject partition = new ManagementObjectSearcher(String.Format(
            "associators of {{Win32_DiskDrive.DeviceID='{0}'}} " +
                  "where AssocClass = Win32_DiskDriveToDiskPartition",

        if (partition != null)
            // associate partitions with logical disks (drive letter volumes)
            ManagementObject logical = new ManagementObjectSearcher(String.Format(
                "associators of {{Win32_DiskPartition.DeviceID='{0}'}} " + 
                    "where AssocClass= Win32_LogicalDiskToPartition",

            if (logical != null)
                // finally find the logical disk entry
                ManagementObject volume = new ManagementObjectSearcher(String.Format(
                    "select FreeSpace, Size, VolumeName from Win32_LogicalDisk " +
                     "where Name='{0}'",

                UsbDisk disk = new UsbDisk(logical["Name"].ToString());
                disk.Model = drive["Model"].ToString();
                disk.Volume = volume["VolumeName"].ToString();
                disk.FreeSpace = (ulong)volume["FreeSpace"];
                disk.Size = (ulong)volume["Size"];


    return disks;

Intercepting Driver State Changes

Now that we can enumerate the currently available USB disk drives, it would be nice to know when one of these goes offline or a new drive comes online. This is the purpose of the UsbManager.DriverWindow class.

The DriverWindow class extends System.Windows.Forms.NativeWindow and is a private class encapsulated by UsbManager. The WndProc method of NativeWindow provides a convenient location to intercept and process Windows messages. The Windows message we need is WM_DEVICECHANGE and its LParam value must be DBT_DEVTYP_VOLUME. The WParam value is also important and we look for two DBT values (and an optional third).

  • DBT_DEVICEARRIVAL - broadcast when a device or piece of media has been inserted and becomes available
  • DBT_DEVICEREMOVECOMPLETE - broadcast when a device or piece of media has been physically removed
  • DBT_DEVICEQUERYREMOVE - broadcast to request permission to remove a device or piece of media; we do not process this message but it provides an opportunity to deny removal of a device

DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE both deliver a DEV_BROADCAST_VOLUME struct. This is actually a DEV_BROADCAST_HDR whose dbcv_devicetype is set to DBT_DEVTYP_VOLUME, so we know we can cast the packet to a DEV_BROADCAST_VOLUME.

    public int dbcv_size;       // size of the struct
    public int dbcv_devicetype; // DBT_DEVTYP_VOLUME
    public int dbcv_reserved;   // reserved; do not use
    public int dbcv_unitmask;   // Bit 0=A, bit 1=B, and so on (bitmask)
    public short dbcv_flags;    // DBTF_MEDIA=0x01, DBTF_NET=0x02 (bitmask)

The dbcv_unitmask field is a bitmask where each of the first 26 low-order bits correspond to a Windows drive letter. Apparently, it is possible to see a device associated with more than one drive letter but we only care about the first available for our use.

DriverWindow fires its own StateChanged event to signal UsbManager. UsbManager then decides if it needs to retrieve information - which it does for new arrivals - and then fires its own StateChanged event to signal consumers.

The StateChanged Event

The demo app attached to this article shows all the power of UsbManager in just a few lines of code. It first enumerates all existing USB disk drives and displays them in a TextBox. It then wires up a handler to the UsbManager.StateChanged event. This event is defined as follows:

public event UsbStateChangedEventHandler StateChanged

Take a look at the StateChanged implementation and you'll notice that the add and remove statements have been extended. This allows us to instantiate a DriverWindow instance only when consumers are listening and then dispose it off when all consumers have stopped listening.

Your handler must be declared as a UsbStateChangedEventHandler as follows:

public delegate void UsbStateChangedEventHandler (UsbStateChangedEventArgs e);

And the UsbStateChangedEventArgs is declared as:

public class UsbStateChangedEventArgs : EventArgs
    public UsbDisk Disk;
    public UsbStateChange State;
  • The State property is an enum specifying one of Added, Removing, or Removed.
  • The Disk property is a UsbDisk instance. If State is Added, then all properties of Disk should be populated. However, if State is Removing or Removed, then only the Name property is populated since we can't detect attributes of a device that no longer exist.


If you found this article helpful and enjoy the iTuner application, please consider donating to support continual improvements of iTuner and, hopefully, more helpful articles. Thanks!


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: Problems USB key with 2 partitions Pin
Dj Andy19-Feb-16 5:49
memberDj Andy19-Feb-16 5:49 
Generalusb dongle - usb etoken not detect Pin
yogarajan2-Sep-10 21:00
memberyogarajan2-Sep-10 21:00 
GeneralRe: usb dongle - usb etoken not detect Pin
stevenmcohn3-Sep-10 1:37
memberstevenmcohn3-Sep-10 1:37 
QuestionCan you definitly distinguish a usb stick from an external usb attached harddisk Pin
jascht26-Aug-10 23:38
memberjascht26-Aug-10 23:38 
AnswerRe: Can you definitly distinguish a usb stick from an external usb attached harddisk Pin
stevenmcohn27-Aug-10 1:22
memberstevenmcohn27-Aug-10 1:22 
GeneralStateChanged not fired... Pin
mpgjunky19-May-10 23:17
membermpgjunky19-May-10 23:17 
GeneralWrong size reported! Pin
branko.rieck13-Apr-10 21:41
memberbranko.rieck13-Apr-10 21:41 
GeneralRe: Wrong size reported! Pin
stevenmcohn14-Apr-10 1:03
memberstevenmcohn14-Apr-10 1:03 
GeneralRe: Wrong size reported! Pin
branko.rieck14-Apr-10 1:19
memberbranko.rieck14-Apr-10 1:19 
GeneralRe: Wrong size reported! Pin
Kelvinloo8823-Aug-10 7:43
memberKelvinloo8823-Aug-10 7:43 
GeneralRe: Wrong size reported! Pin
stevenmcohn23-Aug-10 8:23
memberstevenmcohn23-Aug-10 8:23 
GeneralRe: Wrong size reported! Pin
Kelvinloo8824-Aug-10 9:45
memberKelvinloo8824-Aug-10 9:45 
GeneralRe: Wrong size reported! Pin
Lloyd Atkinson22-Dec-10 3:10
memberLloyd Atkinson22-Dec-10 3:10 
QuestionException error when run in environment. Pin
cpars1-Apr-10 18:57
membercpars1-Apr-10 18:57 
AnswerRe: Exception error when run in environment. Pin
stevenmcohn2-Apr-10 1:19
memberstevenmcohn2-Apr-10 1:19 
GeneralRe: Exception error when run in environment. Pin
Tefik Becirovic17-Jul-10 5:40
memberTefik Becirovic17-Jul-10 5:40 
GeneralRe: Exception error when run in environment. Pin
stevenmcohn18-Jul-10 0:36
memberstevenmcohn18-Jul-10 0:36 
AnswerRe: Exception error when run in environment. Pin
Tefik Becirovic18-Jul-10 23:08
memberTefik Becirovic18-Jul-10 23:08 
GeneralUpdated Source Zip Pin
stevenmcohn15-Mar-10 7:35
memberstevenmcohn15-Mar-10 7:35 
Generalcool Pin
Yves8-Mar-10 13:19
memberYves8-Mar-10 13:19 
GeneralRe: cool Pin
Yves8-Mar-10 13:50
memberYves8-Mar-10 13:50 
GeneralRe: cool Pin
stevenmcohn8-Mar-10 14:10
memberstevenmcohn8-Mar-10 14:10 
GeneralRe: cool Pin
stevenmcohn18-Jul-10 0:37
memberstevenmcohn18-Jul-10 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180618.1 | Last Updated 9 Mar 2010
Article Copyright 2010 by stevenmcohn
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid