2012-08-11 59 views
0

我有1個磁盤分成多個分區。我想獲得每個分區上的可用空間,以確定最佳放置文件的位置。如何從Windows使用C獲得可用磁盤空間?

如何在C中做到這一點?

我一直在嘗試使用此代碼:

__int64 lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes; 
DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters; 

test = GetDiskFreeSpaceEx(
     pszDrive, 
     (PULARGE_INTEGER)&lpFreeBytesAvailable, 
     (PULARGE_INTEGER)&lpTotalNumberOfBytes, 
     (PULARGE_INTEGER)&lpTotalNumberOfFreeBytes 
     ); 

但結果是不正確的。

任何想法?

感謝

+1

其實這是正確的函數調用。結果有什麼問題? – 2012-08-11 21:54:53

+2

http://support.microsoft.com/kb/231497 – Gir 2012-08-11 21:57:29

+0

你有沒有定義和初始化pszDrive? – 2012-08-11 21:58:53

回答

1

能正常工作對我來說:

void main (int argc, wchar_t **argv) 
    { 
     BOOL fResult; 
     unsigned __int64 i64FreeBytesToCaller, 
         i64TotalBytes, 
         i64FreeBytes; 
     fResult = GetDiskFreeSpaceEx (L"C:", 
           (PULARGE_INTEGER)&i64FreeBytesToCaller, 
           (PULARGE_INTEGER)&i64TotalBytes, 
           (PULARGE_INTEGER)&i64FreeBytes); 
     if (fResult) 
     { 
      printf ("\n\nGetDiskFreeSpaceEx reports\n\n"); 
      printf ("Available space to caller = %I64u MB\n", 
        i64FreeBytesToCaller/(1024*1024)); 
      printf ("Total space    = %I64u MB\n", 
        i64TotalBytes/(1024*1024)); 
      printf ("Free space on drive  = %I64u MB\n", 
        i64FreeBytes/(1024*1024)); 
     } 
    } 
+0

回答了你,非常感謝,有一點問題。 – USer22999299 2012-08-13 06:21:42

相關問題