How To Get Free Disk Space In C++ (Qt for Windows)

You can use the following Win32 API function to get the free disk space in C++. Note that this function can be used with Qt for Windows if you add the required dependencies described in this post.

First of all you need to add the following to your Qt project file (.PRO file)

LIBS += -lkernel32

Next, you have to include “Windows.h” file in your header file.

#include "Windows.h"

Now you can use the following function to get the remaining free disk space in the main hard disk partition, in terms of percentage. Notice that you can also use lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters and lpTotalNumberOfClusters to get even more detailed information about the disk.

Also by providing any path or disk address other than NULL to the first parameter of GetDiskFreeSpace function below (the function used in the if clause below) you can get information about other partitions.

int getDiskFreeSpacePercentage()
	DWORD lpSectorsPerCluster,

	if (GetDiskFreeSpace(NULL,
		return int(double(lpNumberOfFreeClusters) / double(lpTotalNumberOfClusters) * 100.0);
		return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.