WinAPI: How to Get the Amount of Space Available on a Disk Volume (GetDiskFreeSpaceEx)
The following method shows how to get the amount of space available on a disk volume. We use a method imported from kernel32: GetDiskFreeSpaceEx:
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)] internal static extern bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes);
If it returns false - there was an exception calling the method. freeBytesForUser is what we are looking for. Starting from .NET 2.0 there is a managed class called System.IO.DriveInfo with property AvailableFreeSpace which uses the same method from kernel32.
Friday, April 18, 2008 9:44 PM