linux(中标麒麟) 获取硬盘信息
中标麒麟使用中,有时候需要获取硬盘大小,防止在使用电脑过程中硬盘空间不足而不自知。
下面提供一个函数获取所有分区的信息:
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
判断当前exe目录所在分区信息:
1 QString GetModuleDir() 2 { 3 QString dir = QApplication::applicationDirPath(); 4 return dir; 5 }
1 //磁盘剩余空间 20201122 sdl 2 double GetDiskRemainingSize(QList<DiskInfo>& listDiskInfo) 3 { 4 double pUsed;//已用空间 5 double pSurplus = -1;//剩余空间 6 float fUsed;//已经使用 7 float fRemaining;//剩余 8 9 QString strCmd = GetModuleDir(); 10 strCmd = "df -k " + strCmd;//直接读取Rbin文件夹 所在分区的信息 11 QProcess process; 12 process.start(strCmd); 13 process.waitForFinished(); 14 process.readLine(); 15 QString strRead = process.readLine(); 16 17 strRead.replace("\n",""); 18 strRead.replace(QRegExp("( ){1,}")," "); 19 QStringList lst = strRead.split(" "); 20 if(lst.size() > 5){ 21 fUsed = lst[2].toFloat() / 1024; 22 fRemaining = lst[3].toFloat() / 1024; 23 QString str = QString("dev/root:be used-%1,remaining-%2").arg(fUsed).arg(fRemaining); 24 LOG(DEBUG) << str; 25 DiskInfo diskInfo; 26 diskInfo.fUsed = fUsed; 27 diskInfo.fRemaining = fRemaining; 28 listDiskInfo.push_back(diskInfo); 29 } 30 31 return pSurplus; 32 }