Get free disk space

bh213 picture bh213 · Sep 8, 2009 · Viewed 135.6k times · Source

Given each of the inputs below, I'd like to get free space on that location. Something like

long GetFreeSpace(string path)

Inputs:

c:

c:\

c:\temp

\\server

\\server\C\storage

Answer

erez dan picture erez dan · Jul 25, 2011

this works for me...

using System.IO;

private long GetTotalFreeSpace(string driveName)
{
    foreach (DriveInfo drive in DriveInfo.GetDrives())
    {
        if (drive.IsReady && drive.Name == driveName)
        {
            return drive.TotalFreeSpace;
        }
    }
    return -1;
}

good luck!