检查磁盘已满

时间:2010-08-19 09:07:06

标签: c++ qt disk

  

可能重复:
  How to determine how much free space on a drive in Qt?

如何使用Qt使用C ++检查磁盘的完整性? 非常感谢

1 个答案:

答案 0 :(得分:0)

显然不是。但我发现了here

#ifdef _WIN32
    #include <windows.h>
#else // linux stuff
    #include <sys/vfs.h>
    #include <sys/stat.h>
#endif // _WIN32

bool getFreeTotalSpace(const QString& sDirPath,double& fTotal, double& fFree)
{
#ifdef _WIN32

    QString sCurDir = QDir::current().absPath();
    QDir::setCurrent( sDirPath );

    ULARGE_INTEGER free,total;
    bool bRes = ::GetDiskFreeSpaceExA( 0 , &free , &total , NULL );
    if ( !bRes ) return false;

    QDir::setCurrent( sCurDir );

    fFree = static_cast<double>( static_cast<__int64>(free.QuadPart) ) / fKB;
    fTotal = static_cast<double>( static_cast<__int64>(total.QuadPart) ) / fKB;

#else //linux

    struct stat stst;
    struct statfs stfs;

    if ( ::stat(sDirPath.local8Bit(),&stst) == -1 ) return false;
    if ( ::statfs(sDirPath.local8Bit(),&stfs) == -1 ) return false;

    fFree = stfs.f_bavail * ( stst.st_blksize / fKB );
    fTotal = stfs.f_blocks * ( stst.st_blksize / fKB );

#endif // _WIN32

    return true;
}