Using DriveInfo class in .NET 2.0

Waqas Anwar
02 March 2008
6907 Views

One of the most common task developers perform in everyday programming is to Working with the file system. This task includes navigating drives, folders and files and get some useful information for the front end application. System.IO namespace in .NET Framework 2.0 provides many classes to work with the file system. One of the class is DriveInfo class. This class represents a drive in the file system.

The following code demonstrates how you can get all the drives of the system using DriveInfo class static/shared method GetDrives() which returns an array of DriveInfo objects.


C#

DriveInfo[] drives = DriveInfo.GetDrives();

foreach (DriveInfo drive in drives)
{
    if (drive.IsReady)
    {
        Console.WriteLine(drive.AvailableFreeSpace);
        Console.WriteLine(drive.DriveFormat);
        Console.WriteLine(drive.RootDirectory);
        Console.WriteLine(drive.TotalFreeSpace);
        Console.WriteLine(drive.TotalSize);
        Console.WriteLine(drive.VolumeLabel);
    }               
}


VB.NET

Dim drives As DriveInfo() = DriveInfo.GetDrives()

For Each drive As DriveInfo In drives
    If drive.IsReady Then
        Console.WriteLine(drive.AvailableFreeSpace)
        Console.WriteLine(drive.DriveFormat)
        Console.WriteLine(drive.RootDirectory)
        Console.WriteLine(drive.TotalFreeSpace)
        Console.WriteLine(drive.TotalSize)
        Console.WriteLine(drive.VolumeLabel)
    End If
Next