February 2, 2008

Calculate Drive Free Space

Calculate the Ammount of Freespace on a Given Drive
private static long FreeSpaceOnDrive(string path)
{
    long res = 0;
    string root = string.Empty;
    if (Path.IsPathRooted(path))
    {
        root = Path.GetPathRoot(path);
        DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
        foreach (DriveInfo drive in drives)
        {
            if (root.Equals(Path.GetPathRoot(drive.Name), 
                StringComparison.InvariantCultureIgnoreCase))
            {
                if (drive.IsReady)
                {
                    res = drive.AvailableFreeSpace;
                    break;
                }
            }
        }
    }
    return res;
}

No comments: