USer22999299
USer22999299

Reputation: 5694

How do I get available disk space from Windows using C?

I have 1 disk that is split into multiple partitions. I want to get the free space available on each partition, to determine where best to put files.

How can I do this in C?

I've been trying to use this code:

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

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

but the results are not correct.

Any ideas ?

Thanks

Upvotes: 1

Views: 4869

Answers (1)

Gir
Gir

Reputation: 849

this works fine for me:

 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));
         }
   }

Upvotes: 4

Related Questions