Using DriveInfo class in .NET 2.0


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​

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments