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#

No comments:

Post a Comment