Detecting removable drive types using C#

Ever wanted to find out the different drives or just the removable drives on the machine ?? The DriveInfo class is the answer to your prayers.

The following code snippet demonstrates how to detect removable drives (Pen drive etc.) using C# code,

DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives )
{
   if( drive.DriveType == DriveType.Removable )
   {
      //do your processing here.
   }
}

The different drive types that can be detected using this approach are (enum values of DriveType enumeration),

Unknown:
The type of drive is unknown.  

NoRootDirectory:
The drive does not have a root directory.  

Removable:
The drive is a removable storage device, such as a floppy disk drive or a USB flash drive.  

Fixed:
The drive is a fixed disk.  

Network:
The drive is a network drive.  

CDRom:
The drive is an optical disc device, such as a CD or DVD-ROM.  

Ram:
The drive is a RAM disk.

Have fun.

Tags: ,

Leave a Reply