This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Getting Connected Drive Information

When writing software that accesses the file system, it can be important to obtain a list of the drives that are available. It is also useful to know further details about each disk, such as the type, capacity and free space for each connected drive.

DriveInfo.GetDrives Method

The .NET framework includes the System.IO namespace, which contains classes that can be used to access drives, folders and files. Within the namespace is the DriveInfo class. Instances of this class hold information relating to a drives that are connected to the computer. The class also includes a static method that retrieves an array of DriveInfo objects, with one item for every currently connected drive.

The code sample below shows an example of the use of the GetDrives method. In this example, all of the currently connected drives are retrieved. The list is traversed using a foreach loop and some information about each item is outputted to the console. The results will vary according to your current configuration.

To use the code sample, ensure that you have added the following using directive to the code file:

using System.IO;

To display the drive information, execute the following code:

DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
    Console.WriteLine("{0} ({1})", drive.Name, drive.DriveType);


C:\ (Fixed)
D:\ (Fixed)
Z:\ (CDRom)


Retrieving a Single Drive

If you wish to obtain information for a single drive, you can create a new instantiate a new DriveInfo object. The class's constructor accepts a single string parameter. This parameter accepts the drive letter of the disk that you wish to interrogate. For example:

DriveInfo drive = new DriveInfo("C");
Console.WriteLine("{0} ({1})", drive.Name, drive.DriveType);

DriveInfo Properties

There are several useful properties of the DriveInfo class in addition to the Name and DriveType properties used above. They include:

  • Name. The name of the drive. This is returned as a string containing the path to the root of the drive, for example, "C:\".
  • DriveType. The DriveType property returns a value from the DriveType enumeration. There are seven possible values for the drive type. These are CDRom, Fixed, Network, NoRootDirectory, Ram, Removable and Unknown. Most of these are self-explanatory. CDRom is returned for CD-ROM, DVD-ROM, CD and DVD Writers and similar drives. NoRootDirectory indicates that a drive does not have an available root directory.
  • IsReady. Provides a Boolean value that indicates if the drive is ready. If a drive is not ready, reading many of the other properties will cause an IOException to be thrown. Drives can be unavailable for various reasons. One of the most common is a CD-ROM drive that has no disk inserted.
  • VolumeLabel. Returns the descriptive label that has been applied to the drive, or the media in the case of removable storage or CD-ROMs. This property is only available when the drive is ready.
  • TotalSize. Gets the total size of the drive in bytes. This property is only available when the drive is ready.
  • TotalFreeSpace. Gets the remaining free space on the drive in bytes. This property is only available when the drive is ready.
  • AvailableFreeSpace. Gets the available free space for the current user in bytes. This value can be different from the TotalFreeSpace value if the drive in question is using disk quotas. This property is only available when the drive is ready.
  • DriveFormat. This property returns a string containing the name of the disk's format. The name depends upon the operating system that the disk resides upon and the options set when the disk was initialised. For Windows drives, the most common types are FAT32 and NTFS.
  • RootDirectory. Provides a DirectoryInfo object containing the details of the root folder for the drive.
3 March 2009