中标麒麟使用中,有时候需要获取硬盘大小,防止在使用电脑过程中硬盘空间不足而不自知。
下面提供一个函数获取所有分区的信息:
1 struct DiskInfo{ 2 float fUsed; 3 float fRemaining; 4 };
1 //磁盘剩余空间 20201122 sdl 2 double GetDiskRemainingSize(QList<DiskInfo>& listDiskInfo) 3 { 4 double pUsed;//已用空间 5 double pSurplus = -1;//剩余空间 6 float fUsed;//已经使用 7 float fRemaining;//剩余 8 QProcess process; 9 process.start("df -k"); 10 process.waitForFinished(); 11 process.readLine(); 12 while(!process.atEnd()){ 13 QString str = process.readLine();//查询/dev/root空间 14 if(str.startsWith("/dev/root")){//"/dev/root" 15 str.replace("\n",""); 16 str.replace(QRegExp("( ){1,}")," "); 17 QStringList lst = str.split(" "); 18 if(lst.size() > 5){ 19 fUsed = lst[2].toFloat() / 1024; 20 fRemaining = lst[3].toFloat() / 1024; 21 QString str = QString("dev/root:be used-%1,remaining-%2").arg(fUsed).arg(fRemaining); 22 LOG(DEBUG) << str; 23 DiskInfo diskInfo; 24 diskInfo.fUsed = fUsed; 25 diskInfo.fRemaining = fRemaining; 26 listDiskInfo.push_back(diskInfo); 27 } 28 } 29 if(str.startsWith("/dev/sd")){ 30 str.replace("\n",""); 31 str.replace(QRegExp("( ){1,}")," "); 32 QStringList lst = str.split(" "); 33 if(lst.size() > 5){ 34 fUsed = lst[2].toFloat() / 1024; 35 fRemaining = lst[3].toFloat() / 1024; 36 QString str = QString("dev/root:be used-%1,remaining-%2").arg(fUsed).arg(fRemaining); 37 LOG(DEBUG) << str; 38 DiskInfo diskInfo; 39 diskInfo.fUsed = fUsed; 40 diskInfo.fRemaining = fRemaining; 41 listDiskInfo.push_back(diskInfo); 42 } 43 } 44 } 45 return pSurplus; 46 }
另外,提供两个示例程序参考:
https://files-cdn.cnblogs.com/files/warmlight/demo1%E7%B1%BB%E4%BC%BC%E4%BA%8EDiskSize.zip
https://files-cdn.cnblogs.com/files/warmlight/DiskSize.rar