Click to See Complete Forum and Search --> : Drive Space!


dave1979bell
October 5th, 2005, 03:23 AM
does anyone know what i would use to get the free space on my local hard drive.

I am looking for something that I can pass the drive letter into.

something that would work like this... (i know this is not actuall code just givving an example of how i would expect it to function)



intFreeSpace = localmachine.drivespace.freespace(driveletter);

Thanks in Advance,

David

wildfrog
October 5th, 2005, 03:52 AM
You could do something like this:

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Free space {0}\n", GetDiskSpace("c:"));
}

static UInt64 GetDiskSpace(string drive)
{
// make it upper
drive = drive.ToUpper();

// enumerate disks
ManagementClass pDiskClass = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection pDisks = pDiskClass.GetInstances();

foreach (ManagementObject pDisk in pDisks)
if (drive.CompareTo((string)pDisk["name"]) == 0)
return (UInt64) pDisk["FreeSpace"];

// disk not found
return 0;
}

- petter