QStorageInfo类提供了系统当前挂载的存储和驱动器的相关信息,包括它们的空间,挂载点,标签名,文件系统名。
可以创建一个QStorageInfo对象,使用其静态方法mountedVolumes()来得到当前系统中挂载的所有文件系统的列表;还可以使用root()静态方法,来获取根文件系统的相关信息,在Linux平台是即为"/"目录,在Windows平台上即为系统盘。
QString dir = QDir::currentPath();
quint64 space = 0;
#if defined(Q_OS_WIN32)
QList<QStorageInfo> storageInfoList = QStorageInfo::mountedVolumes();//得到当前系统中挂载的所有文件系统的列表
foreach(QStorageInfo storage,storageInfoList) //根据列表查找
{
if(dir.contains(storage.rootPath()))//windows上获取系统盘,比如C盘,这里通过比较查看dir属于哪一个盘
{
space = storage.bytesAvailable()/1024/1024; //获取这个盘的大小
break;
}
}
/*
foreach(QStorageInfo storage,storageInfoList)
{
if(QCoreApplication::applicationDirPath().contains(storage.rootPath()))//获取运行目录的盘符,在哪一个盘
{
space = storage.bytesAvailable()/1024/1024;
break;
}
}
*/
#else
//linux下获取磁盘大小,其实就是获取"/"根目录的大小
int state;
struct statvfs vfs;
fsblkcnt_t block_size = 0;
fsblkcnt_t free_size;
state = statvfs("/", &vfs);
if(state < 0)
{
printf("read statvfs error!!!\n");
}
block_size = vfs.f_bsize;
free_size = vfs.f_bfree * block_size;
space = free_size/1024/1024;
#endif
dir.contains(storage.rootPath())
contains是QString中查看是否含有子字符串的方法,storage.rootPath()是获取这个盘符的字符串,这样就可以检测出dir属于哪个盘符
测试例程
QString dir = QDir::currentPath();
quint64 space = 0;
QList<QStorageInfo> storageInfoList = QStorageInfo::mountedVolumes();//得到当前系统中挂载的所有文件系统的列表
foreach(QStorageInfo storage,storageInfoList) //根据列表查找
{
if(dir.contains(storage.rootPath()))//windows上获取系统盘,比如C盘,这里通过比较查看dir属于哪一个盘
{
qDebug()<<"dir:"<<dir;
qDebug()<<"storage.rootPath:"<<storage.rootPath();
space = storage.bytesAvailable()/1024/1024; //获取这个盘的大小
break;
}
}
根据上面的打印信息可以看到用这个方法可以查找到目录属于哪个盘符,进而可以获取这个盘符的大小
demo地址链接:
https://download.csdn.net/download/qq_40170041/85625289
这个是获取盘符的所有信息
QList<QStorageInfo> list = QStorageInfo::mountedVolumes();
qDebug() << "Volume Num: " << list.size();
for(QStorageInfo& si : list)
{
qDebug() << "Name: " << si.name();
qDebug() << "Block Size: " << si.blockSize();
qDebug() << "Bytes Avaliable: " << si.bytesAvailable();
qDebug() << "Bytes Free: " << si.bytesFree();
qDebug() << "Bytes Total: " << si.bytesTotal();
qDebug() << "Display Name: " << si.displayName();
qDebug() << "File System Type: " << si.fileSystemType();
qDebug() << "Is ReadOnly: " << si.isReadOnly();
qDebug() << "Is Root: " << si.isRoot();
}
qDebug() << QStorageInfo::root().device();
上面linux获取磁盘大小是获取的根目录下的磁盘大小,此时挂载的U盘等也会计算在内,如果只计算某路径所在的磁盘大小,就需要获取硬盘信息的statfs结构,通过statfs结构的信息计算出路径所在的磁盘使用情况。
#include <stdio.h>
#include <sys/statfs.h>
#include <sys/vfs.h>
#include <errno.h>
int main(int argc, char *argv[])
{
struct statfs disk_info;
char *path = "/home/";
int ret = 0;
if (argc == 2)
{
path = argv[1];
}
if (ret == statfs(path, &disk_info) == -1)
{
fprintf(stderr, "Failed to get file disk infomation,\
errno:%u, reason:%s\n", errno, strerror(errno));
return -1;
}
long long total_size = disk_info.f_blocks * disk_info.f_bsize;
long long available_size = disk_info.f_bavail * disk_info.f_bsize;
long long free_size = disk_info.f_bfree * disk_info.f_bsize;
//输出每个块的长度,linux下内存块为4KB
printf("block size: %ld bytes\n", disk_info.f_bsize);
//输出块个数
printf("total data blocks: %ld \n", disk_info.f_blocks);
//输出path所在磁盘的大小
printf("total file disk size: %d MB\n",total_size >> 20);
//输出非root用户可以用的磁盘空间大小
printf("avaiable size: %d MB\n",available_size >> 20);
//输出硬盘的所有剩余空间
printf("free size: %d MB\n",free_size >> 20);
//输出磁盘上文件节点个数
printf("total file nodes: %ld\n", disk_info.f_files);
//输出可用文件节点个数
printf("free file nodes: %ld\n", disk_info.f_ffree);
//输出文件名最大长度
printf("maxinum length of file name: %ld\n", disk_info.f_namelen);
return 0;
}
程序中的>>20
相当于除以2的20次方也就是除以1024的平方(free_size / 1024 / 1024
)