CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Threaded View

  1. #1
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Windows SDK File System: How can I check the free space on a partition?

    Q: How can I check the free space on a partition?

    A:

    Code:
    typedef BOOL (WINAPI *PFNGETDISKFREESPACEEX)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
    
    #include <iostream>
    
    HINSTANCE hModule = ::LoadLibrary("KERNEL32.DLL");
    if(hModule)
    {
      PFNGETDISKFREESPACEEX pDiskFreeSpaceEx = NULL;
    
      // Determine function to use
      pDiskFreeSpaceEx = reinterpret_cast<PFNGETDISKFREESPACEEX>(::GetProcAddress(hModule,
                                                                                  "GetDiskFreeSpaceExA"));
      if(!pDiskFreeSpaceEx)
      {
        DWORD dwSectorsPerCluster = 0;
        DWORD dwBytesPerSector    = 0; 
        DWORD dwFreeClusters      = 0; 
        DWORD dwClusters          = 0; 
    
        if(::GetDiskFreeSpace(strRootPath.c_str(),
                              &dwSectorsPerCluster,
                              &dwBytesPerSector,
                              &dwFreeClusters,
                              &dwClusters) == TRUE)
          std::cout << "Available disk space = "
                    << dwFreeClusters * dwBytesPerSector * dwSectorsPerCluster << " MB" << std::endl;
        else
          // Error
      }
      else
      {
        ULARGE_INTEGER uliFreeBytesAvailableToCaller;
        ULARGE_INTEGER uliTotalNumberOfBytes;
        ULARGE_INTEGER uliTotalNumberOfFreeBytes;
    
        if(::GetDiskFreeSpaceEx(strRootPath.c_str(),
                                &uliFreeBytesAvailableToCaller,
                                &uliTotalNumberOfBytes,
                                &uliTotalNumberOfFreeBytes) == TRUE)
          std::cout << "Available disk space = "
                    << static_cast<int>(static_cast<__int64>(uliFreeBytesAvailableToCaller.QuadPart /
                                                             (1024 * 1024)))
                    << " MB" << std::endl;
        else
          // Error
      }
    
      // Release library
      ::FreeLibrary(hModule);
    }
    else
      // Error -> call ::GetLastError()
    Last edited by Andreas Masur; July 24th, 2005 at 05:23 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured