dwj*_*dwj 15 c++ filesystems qt
我正在使用Qt并希望获得可用的可用磁盘空间的平台无关方式.
我知道在Linux中我可以使用statfs,在Windows中我可以使用GetDiskFreeSpaceEx().我知道助推有办法,boost::filesystem::space(Path const & p).
但我不想要那些.我在Qt,并希望以Qt友好的方式做到这一点.
我看了看QDir,QFile,QFileInfo-没有!
Mr.*_*fee 24
我知道这是一个相当古老的话题,但有人仍然觉得它很有用.
从QT 5.4开始,QSystemStorageInfo已经停止了,而是有一个新的类QStorageInfo,它使整个任务变得非常简单,并且它是跨平台的.
QStorageInfo storage = QStorageInfo::root();
qDebug() << storage.rootPath();
if (storage.isReadOnly())
qDebug() << "isReadOnly:" << storage.isReadOnly();
qDebug() << "name:" << storage.name();
qDebug() << "fileSystemType:" << storage.fileSystemType();
qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB";
qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB";
Run Code Online (Sandbox Code Playgroud)
已从QT 5.5文档中的示例复制代码
Vio*_*ffe 12
Qt 5.4中引入的新QStorageInfo类可以执行此操作(以及更多).它是Qt Core模块的一部分,因此不需要额外的依赖项.
#include <QStorageInfo>
#include <QDebug>
void printRootDriveInfo() {
QStorageInfo storage = QStorageInfo::root();
qDebug() << storage.rootPath();
if (storage.isReadOnly())
qDebug() << "isReadOnly:" << storage.isReadOnly();
qDebug() << "name:" << storage.name();
qDebug() << "filesystem type:" << storage.fileSystemType();
qDebug() << "size:" << storage.bytesTotal()/1024/1024 << "MB";
qDebug() << "free space:" << storage.bytesAvailable()/1024/1024 << "MB";
}
Run Code Online (Sandbox Code Playgroud)
当我写这个问题时(在QTBUG-3780上投票之后)我写回来了; 我想我会从头开始拯救某人(或我自己).
这是Qt 4.8.x.
#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
Run Code Online (Sandbox Code Playgroud)
用法:
// 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17846 次 |
| 最近记录: |