QT中wmic diskdrive get serialnumber获得硬盘号的同时分别判断是否为移动硬盘
时间: 2023-04-09 15:04:28 浏览: 89
可以使用以下代码来实现:
```cpp
#include <QProcess>
#include <QDebug>
QStringList getSerialNumbers()
{
QStringList serialNumbers;
QProcess process;
process.start("wmic diskdrive get serialnumber");
process.waitForFinished();
QString output = process.readAllStandardOutput();
QStringList lines = output.split("\r\r\n");
for (int i = 1; i < lines.size(); i++) {
QString line = lines.at(i).trimmed();
if (!line.isEmpty()) {
serialNumbers.append(line);
}
}
return serialNumbers;
}
bool isRemovableDrive(const QString &serialNumber)
{
QProcess process;
process.start("wmic logicaldisk where \"VolumeSerialNumber='" + serialNumber + "'\" get drivetype");
process.waitForFinished();
QString output = process.readAllStandardOutput();
QStringList lines = output.split("\r\r\n");
if (lines.size() == 2) {
QString line = lines.at(1).trimmed();
if (line == "2") {
return true;
}
}
return false;
}
int main(int argc, char *argv[])
{
Q_UNUSED(argc);
Q_UNUSED(argv);
QStringList serialNumbers = getSerialNumbers();
for (const QString &serialNumber : serialNumbers) {
bool isRemovable = isRemovableDrive(serialNumber);
qDebug() << "Serial number:" << serialNumber << "Is removable:" << isRemovable;
}
return 0;
}
```
这段代码可以获取所有硬盘的序列号,并判断它们是否为移动硬盘。
阅读全文