qt上可以通过QStorageInfo类获取挂载磁盘的信息(本质上所有能用的磁盘都是挂载到电脑上的,所以可以获取到所有磁盘信息)
直接上代码
QList<QStorageInfo> list = QStorageInfo::mountedVolumes();
qDebug() << "Volume Num: " << list.size();
for(QStorageInfo& si : list)
{
qDebug() <<"---------------------------";
qDebug() << "Name: " << si.name();
qDebug() << "Block Size: " << si.blockSize();
qDebug() << "Bytes Avaliable: " << si.bytesAvailable()/1024/1024/1024; //字节转G,除以三个1024
qDebug() << "Bytes Free: " << si.bytesFree()/1024/1024/1024;
qDebug() << "Bytes Total: " << si.bytesTotal()/1024/1024/1024;
qDebug() << "Display Name: " << si.displayName();
qDebug() << "File System Type: " << si.fileSystemType();
qDebug() << "Is ReadOnly: " << si.isReadOnly();
qDebug() << "Is Root: " << si.isRoot();
qDebug() << "Is Valid: " << si.isValid();
qDebug() <<"---------------------------";
}
qDebug() << QStorageInfo::root().displayName(); //linux上是根目录,windows上是系统盘
除了以上的方法,qt官方文档都可以查得到,用起来也比较简单
linux上的演示结果:
Volume Num: 11
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 3
Bytes Free: 3
Bytes Total: 3
Display Name: "/run"
File System Type: "tmpfs"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 73
Bytes Free: 73
Bytes Total: 99
Display Name: "/"
File System Type: "xfs"
Is ReadOnly: false
Is Root: true
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 29
Bytes Free: 29
Bytes Total: 29
Display Name: "/var"
File System Type: "xfs"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 583
Bytes Free: 583
Bytes Total: 599
Display Name: "/home"
File System Type: "xfs"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 49
Bytes Free: 49
Bytes Total: 49
Display Name: "/boot"
File System Type: "xfs"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 8192
Bytes Avaliable: 9
Bytes Free: 9
Bytes Total: 9
Display Name: "/boot/efi"
File System Type: "vfat"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 0
Bytes Free: 0
Bytes Total: 0
Display Name: "/run/user/42"
File System Type: "tmpfs"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 0
Bytes Free: 0
Bytes Total: 0
Display Name: "/run/user/0"
File System Type: "tmpfs"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 13
Bytes Free: 14
Bytes Total: 14
Display Name: "/run/media/root/25367eb3-94ba-45fb-bbc7-ad356ece4484"
File System Type: "ext4"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
---------------------------
Name: ""
Block Size: -1
Bytes Avaliable: 0
Bytes Free: 0
Bytes Total: 0
Display Name: "/root/thinclient_drives"
File System Type: "fuse.xrdp-chansrv"
Is ReadOnly: false
Is Root: false
Is Root: false
---------------------------
---------------------------
Name: ""
Block Size: 4096
Bytes Avaliable: 13
Bytes Free: 14
Bytes Total: 14
Display Name: "/mnt/vol0"
File System Type: "ext4"
Is ReadOnly: false
Is Root: false
Is Root: true
---------------------------
"/"
windows上演示结果:
Volume Num: 4
---------------------------
Name: ""
Block Size: -1
Bytes Avaliable: 13
Bytes Free: 13
Bytes Total: 119
Display Name: "C:/"
File System Type: "NTFS"
Is ReadOnly: false
Is Root: true
Is Valid: true
---------------------------
---------------------------
Name: ""
Block Size: -1
Bytes Avaliable: 80
Bytes Free: 80
Bytes Total: 406
Display Name: "D:/"
File System Type: "NTFS"
Is ReadOnly: false
Is Root: false
Is Valid: true
---------------------------
---------------------------
Name: ""
Block Size: -1
Bytes Avaliable: 133
Bytes Free: 133
Bytes Total: 405
Display Name: "E:/"
File System Type: "NTFS"
Is ReadOnly: false
Is Root: false
Is Valid: true
---------------------------
---------------------------
Name: ""
Block Size: -1
Bytes Avaliable: 0
Bytes Free: 0
Bytes Total: 0
Display Name: "F:/"
File System Type: ""
Is ReadOnly: false
Is Root: false
Is Valid: false
---------------------------
"C:/"
码字不易,如果这篇博客对你有帮助,麻烦点赞收藏,非常感谢!有不对的地方,可以评论区交流。