int getFreeSpace(wstring wstrWorkingFolder) { int nFreeSpace = 0; HINSTANCE hModule = ::LoadLibrary(L"KERNEL32.DLL"); if(hModule) { PFNGETDISKFREESPACEEX pDiskFreeSpaceEx = NULL; // Determine function to use pDiskFreeSpaceEx = reinterpret_cast(::GetProcAddress(hModule, "GetDiskFreeSpaceExA")); if(!pDiskFreeSpaceEx) { DWORD dwSectorsPerCluster = 0; DWORD dwBytesPerSector = 0; DWORD dwFreeClusters = 0; DWORD dwClusters = 0; if(::GetDiskFreeSpace(wstrWorkingFolder.c_str(), &dwSectorsPerCluster, &dwBytesPerSector, &dwFreeClusters, &dwClusters) == TRUE) { nFreeSpace = dwFreeClusters * dwBytesPerSector * dwSectorsPerCluster; } } else { ULARGE_INTEGER uliFreeBytesAvailableToCaller; ULARGE_INTEGER uliTotalNumberOfBytes; ULARGE_INTEGER uliTotalNumberOfFreeBytes; if(::GetDiskFreeSpaceEx(wstrWorkingFolder.c_str(), &uliFreeBytesAvailableToCaller, &uliTotalNumberOfBytes, &uliTotalNumberOfFreeBytes) == TRUE) { nFreeSpace = static_cast<__int64>(uliFreeBytesAvailableToCaller.QuadPart / (1024 * 1024)); } } // Release library ::FreeLibrary(hModule); } return nFreeSpace; }
2012年1月9日 星期一
[C++] How to get free disk space
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言