网站首页 > 博客文章 正文
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)
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
- 上一篇: Qt获取全屏(qt全屏后其它窗口无法弹出)
- 下一篇: Qt开源类库集合(qt github开源项目)
猜你喜欢
- 2025-06-09 详解RTP协议之H264封包和解包实战
- 2025-06-09 Qt开发经验小技巧146-150(qt开发项目)
- 2025-06-09 Qt Concurrent的使用(qt线程使用)
- 2025-06-09 Qt编写全能播放组件(支持ffmpeg2/3/4/5/6/Qt4/5/6)
- 2025-06-09 「Qt入门第24篇」 数据库(四)SQL查询模型QSqlQueryModel
- 2025-06-09 C++解析头文件-Qt自动生成信号声明
- 2025-06-09 这个C++ Qt 文件传输方案性能飙升300%,GitHub冲上热榜
- 2025-06-09 Qt之QTableview显示及单元格内容获取
- 2025-06-09 Qt编程进阶(25):Qt对Excel的基本读写操作
- 2025-06-09 Qt:QFile类(qt中的类是什么)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)