tags:

views:

522

answers:

2

I'm using Qt and want a platform-independent way of getting the available free disk space.

I know in Linux I use statfs and in Windows I use GetDiskFreeSpaceEx(). I know boost has a way (boost::filesystem::space(Path const & p);).

I don't want those. I'm in Qt and would like to do it in a Qt-friendly way.

I looked at QDir, QFile, QFileInfo -- nothing!

+3  A: 

There is nothing in Qt at time of writing.

Consider commenting on or voting for QTBUG-3780.

Intransigent Parsnip
It's unfortunate that nothing has changed about this since the mailing list posts I found from 2004/2005. Voted up on Qt's site.
dwj
A: 

I wrote this back when I wrote the question (after voting on QTBUG-3780); I figure I'll save someone (or myself) from doing this from scratch.

#ifdef WIN32
/*
 * getDiskFreeSpaceInGB
 *
 * Returns the amount of free drive space for the given drive in GB. The
 * value is rounded to the nearest integer value.
 */
int getDiskFreeSpaceInGB( LPCWSTR drive )
{
    ULARGE_INTEGER freeBytesToCaller;
    freeBytesToCaller.QuadPart = 0L;

    if( !GetDiskFreeSpaceEx( drive, &freeBytesToCaller, NULL, NULL ) )
    {
        qDebug() << "ERROR: Call to GetDiskFreeSpaceEx() failed.";
    }

    int freeSpace_gb = freeBytesToCaller.QuadPart / B_per_GB;
    qDebug() << "Free drive space: " << freeSpace_gb << "GB";

    return freeSpace_gb;
}
#endif

Usage:

// Check available hard drive space
#ifdef WIN32
        // The L in front of the string does some WINAPI magic to convert
        // a string literal into a Windows LPCWSTR beast.
        if( getDiskFreeSpaceInGB( L"c:" ) < MinDriveSpace_GB )
        {
            errString = "ERROR: Less than the recommended amount of free space available!";
            isReady = false;
        }
#else
#    pragma message( "WARNING: Hard drive space will not be checked at application start-up!" )
#endif
dwj