How to get local disk used space


Hi

How to get local disk ( c:\ or d:\ ) used space in windows.
Any API ?
I had this problem a while back, here's the code I wrote for it. Hope it helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <windows.h> 
#include <tchar.h>

int __cdecl _tmain(__in int argc, __in_ecount_z(argc) TCHAR* argv[], __in_z TCHAR* envp[]) 
{
	PULARGE_INTEGER pulAvailable = 0, pulTotal = 0, pulFree = 0;

	if(!GetDiskFreeSpaceEx(NULL, &pulAvailable, &pulTotal, &pulFree))
	{ 
		DWORD	dwSectorsPerCluster = 0, dwBytesPerSector = 0;
		DWORD	dwFreeClusters = 0, dwTotalClusters = 0; 
		
		if(!GetDiskFreeSpaceA(NULL, &dwSectorsPerCluster, 
			&BytesPerSector, &FreeClusters, &TotalClusters)) 
		{ 
			_tprintf(_T("Couldn't get free disk space..")); 
			return EXIT_FAILURE; 
		} 

		dwFreeBytes = dwFreeClusters * dwSectorsPerCluster * dwBytesPerSector; 
		dwTotalBytes = dwTotalClusters * dwSectorsPerCluster * dwBytesPerSector; 
		// do something with dwFreeBytes & dwTotalBytes
	} 

	// do something with pulAvailable, pulTotal, & pulFree
	return EXIT_SUCCESS; 
}
Last edited on
See the classic MSDN code for GetDiskFreeSpaceEx in the KB
Topic archived. No new replies allowed.