Using DriveInfo class in .NET

You are currently viewing Using DriveInfo class in .NET

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
READ ALSO:  Using SqlConnectionStringBuilder class in C#

This Post Has 2 Comments

  1. mehood ahmed

    AOA
    thanks sir
    your site is a wonderful work and made something easy for me.

    not confusions in it.

  2. Kamyar

    hi everyone
    I don’t know how I can solve the problem while I want to read the removeable drive because all of them they are not ready so how I can read volumeLable.

Leave a Reply