The .NET Framework v2.0 did a nice job filling many holes in the System.IO namespace, especially when it comes to managing the file system. One such addition is the DriveInfo class, which enables you to determine what drives are available, their type, capacity and available free space.

Use the following code to obtain a list of all drives installed on a PC:

DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
    string label = drive.IsReady ?
        String.Format( " - {0}", drive.VolumeLabel ) : null;
    Console.WriteLine( "{0} - {1}{2}", drive.Name, drive.DriveType, label );

The output from the code above may look like this:

C: – Fixed – WinXP
D: – Fixed – Data
E: – CDRom
F: – CDRom
G: – Removable
H: – Removable
T: – Fixed – Temp
U: – Fixed – Backups
V: – Fixed – VMs
W: – Fixed – Vista

Notice the use of the IsReady property, which indicates whether the drive volume is mounted. For example, it indicates that a CD is inserted in a CD drive, or that a removable storage device is ready for read/write operations. This is necessary because querying a drive using DriveInfo will raise an IOException if the drive is not ready. Hence, the code above reads the VolumeLabel property only if the drive is ready. Note that the Name and DriveType properties are available at all times, even if the drive is not ready.

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot