如何使用C从Windows获取可用磁盘空间?

时间:2012-08-11 21:52:09

标签: c windows

我有一个分为多个分区的磁盘。我希望获得每个分区上的可用空间,以确定最佳放置文件的位置。

我怎样才能在C中做到这一点?

我一直在尝试使用此代码:

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

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

但结果不正确。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分: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));
         }
   }