思路:先获取所有磁盘号,再获取它的剩余空间,将磁盘号和剩余空间放入到map当中。最后遍历map找到剩余空间最大的那个。
//获取最大剩余空间磁盘
QString GetMaxFreeDisk()
{
QFileInfoList list = QDir::drives(); //获取所有磁盘
QMap<quint64, QString> diskmap;
quint64 maxSizec = 0;
for (int i = 0; i < list.count(); i++)
{
QString str = list.at(i).absoluteDir().absolutePath(); //磁盘号
if (str.isEmpty())
{
continue;
}
LPCWSTR strDriver = (LPCWSTR)str.utf16();
ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace;
//调用函数获取磁盘参数
GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace);
quint64 freeSize = static_cast<quint64>(totalFreeDiskSpace.QuadPart);
diskmap.insert(freeSize, str);
if(maxSizec < freeSize)
{
maxSizec = freeSize;
}
}
QString diskName = diskmap.value(maxSizec);
diskName = diskName.replace("/", "\\");
return diskName;
}