Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Getting free Disk space Using Win32 System Services

Author: DevASP
Download Source Code : 635_GetFreeSpace

The GetDiskFreeSpace function returns information about the number of clusters or free clusters on a disk. Clusters are sets of sectors grouped together as a logical unit.

We can find out the maximum size of any drive, along with its available free space, using the Win32 System Services API  GetDiskFreeSpace.There are two ways that you might use this information. The most obvious use is to detect how much free space is available on a drive before writing a file to it. For example, if you know you are about to write 6 megabytes of information to a drive, you might first check to make sure that amount of space is available. This still is no guarantee:


In a multi-tasking system, two processes can write to the disk at the same time, so the disk space may get consumed by other processes before you completely write the file. However, if you check and see that only 12K of space is available, you can warn the user. The second way to use the information is to check for cluster size. No file can be smaller than the size of one cluster, so if the cluster size is very large on a drive, you might want to take some steps to avoid writing a large number ofvery small files. Note that the product of the sector size, the number of sectors per cluster, and the number of clusters can potentially overflow a 32-bit integer when the NT file system is in use. When checking the disk size, you may want to consider looking only at the free cluster number.The number of free clusters will certainly fall well below 4 billion for the foreseeable future. Alternately, use Microsoft's 64-bit math library found in  




The signature of this function looks like this.


                              Returns information about the size of a drive and its free space


BOOL GetDiskFreeSpace(LPTSTR rootName,LPDWORD freeClusters,LPDWORD clusters);



rootName                       The name of the logical drive

sectorsPerCluster           The number of sectors in each cluster

bytesPerSector              The number of bytes per sector

freeClusters                   The number of unused clusters on the drive

clusters                         The number of clusters per drive

Returns a Boolean indicating success.


The code sample attached with this article performs this operation like this.



BOOL success;

      unsigned long sectorsPerCluster, bytesPerSector,

            freeClusters, clusters;


      // Get disk space for drive C

      success = GetDiskFreeSpace( "c:\\",


            &bytesPerSector, &freeClusters, &clusters);

      if (!success)






      // Output full disk size and free space

      CString strSPC;

      unsigned long spc=sectorsPerCluster*bytesPerSector*clusters;

      strSPC.Format("Disk Size %ul Bytes",spc);

      MessageBox(strSPC,"Disk Size");


      CString strFS;

      unsigned long fs=sectorsPerCluster*bytesPerSector * freeClusters;

      strFS.Format("Free Space %ul Bytes",fs);

        MessageBox(strFS,"Free Space");


This will show you the total size of Disc Drive “C” and the Free space available in Bytes.

Article Comments
thanks just what I needed!

Posted on 8/20/2010 1:21:29 PM by Matt

Add Article Comment:
Name :
Email Address :
Comments :
<< Getting Volume Information: Using Win32 System Services

Disclaimer - Privacy
© 2002-2018