Programmatically determine Total Free Space available on your Hard Drive

The DriveInfo.AvailableFreeSpace property indicates the amount of available free space on a drive. Here’s how to use this property to programmatically retrieve the total free space of all logical drives (including CD/DVD drives) on your machine.

C#

static void Main(string[] args)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
try
{
Console.WriteLine("{0} has {1} MB free space available",
drive.RootDirectory, (drive.AvailableFreeSpace / 1024) / 1024);
}
catch (IOException io)
{
Console.WriteLine(io.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.ReadLine();
}

VB.NET

Shared Sub Main(ByVal args() As String)
For Each drive As DriveInfo In DriveInfo.GetDrives()
Try
Console.WriteLine("{0} has {1} MB free space available", _
drive.RootDirectory, (drive.AvailableFreeSpace \ 1024) / 1024)
Catch io As IOException
Console.WriteLine(io.Message)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Next
drive
Console.ReadLine()
End Sub

OUTPUT

FreeSpace C#



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: