CPP: Windows: GetDiskFreeSpaceExW
Retrieves information about the amount of space that is available on a disk volume, which is the total amount of space, the total amount of free space, and the total amount of free space available to the user that is associated with the calling thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <Windows.h> | |
int main() | |
{ | |
ULARGE_INTEGER FreeBytesAvailableToCaller, TotalNumberOfBytes, TotalNumberOfFreeBytes; | |
auto ret = GetDiskFreeSpaceExW(L"C:\\", &FreeBytesAvailableToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes); | |
if (ret) { | |
auto MB = 1.0 / (1024 * 1024 * 1024); | |
std::wcout << std::setprecision(4); | |
std::wcout << "FreeBytesAvailableToCaller: " << FreeBytesAvailableToCaller.QuadPart*MB << std::endl; | |
std::wcout << "TotalNumberOfBytes(MB): " << TotalNumberOfBytes.QuadPart * MB << std::endl; | |
std::wcout << "TotalNumberOfFreeBytes: " << TotalNumberOfFreeBytes.QuadPart*MB << std::endl; | |
return 0; | |
} | |
else { | |
return -1; | |
} | |
} |
Notes:
- Tested using Visual Studio 2022 (17.2.6) C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexw