VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass() įoreach (Volume device in volumeDeviceClass.Devices) This is how you can eject all USB volumes for example: A volume has a LogicalDrive property which is filled if it has a drive letter assigned
Note also the code has been designed so it could be extended to other devices, not only volumes and disks Note there is no Disk class, because in this project, a Disk has no specific property, compared to a Device.
VolumeDeviceClass: Represents all volume devices in the system.DiskDeviceClass: Represents all disk devices in the system.DeviceClass: An abstract class that represents a physical device class.The classes there are described in the following class diagram:Īs you can see, there are 5 main classes (each class is defined in its own respective. There is a folder called Library that contains the heart of it, an object oriented API to handle USB disks.
The project is a Visual Studio 2005 (.NET 2.0) Windows form project. Device is a base type, and Volume derives from it.
The Windows DDK (Driver Development Kit): A driver development kit that provides a build environment, tools, driver samples, and documentation to support driver development for Windows.Backgroundįirst of all, let's talk about the various Windows API involved here, apart from Win32, and there are many:
NET Winforms project that includes reusable source code.Īnd by the way, as a side benefit, I will also explain how to read the hardware serial number of those disks, a recurring question on newsgroups and forums. Well, this is the main reason for this article, I think the subject really deserves one. I sure did not think I would have to switch between kernel driver control codes, the Windows setup and Configuration Manager APIs, WMI, etc. When I started this work, I really did not think where it would lead me to.
There are a lot of underlying notions, at the frontier of kernel driver development, one has to understand to achieve what may seem a simple task. Although removing a USB stick using the Windows shell is quite easy, it tends to be quite difficult programmatically.