Get the Total Free Space on a Drive - CSharp File IO

CSharp examples for File IO:Directory

Description

Get the Total Free Space on a Drive

Demo Code


using System;//from   w  ww .  jav  a 2 s . c  om
using System.IO;

static class MainClass
{
    static void Main(string[] args)
    {
        DriveInfo drive1 = new DriveInfo("c:/");

        Console.WriteLine(drive1.AvailableFreeSpace / 1024);



        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            try
            {
                Console.WriteLine(
                    "{0} - {1} KB",
                    drive.RootDirectory,
                    drive.AvailableFreeSpace / 1024
                    );
            }
            catch (IOException) // network drives may not be available
            {
                Console.WriteLine(drive);
            }
        }
    }
}

Result


Related Tutorials