QT中wmic diskdrive get serialnumber获得硬盘号的同时判断是否为移动硬盘
时间: 2023-04-09 14:04:20 浏览: 333
可以使用以下代码来实现:
```cpp
QProcess process;
process.start("wmic diskdrive get mediatype,serialnumber");
process.waitForFinished();
QString output = process.readAllStandardOutput();
if (output.contains("External") || output.contains("Removable")) {
qDebug() << "This is a removable disk.";
} else {
qDebug() << "This is not a removable disk.";
}
```
这段代码会执行 `wmic diskdrive get mediatype,serialnumber` 命令,并读取输出结果。如果输出结果中包含 "External" 或 "Removable",则说明这是一个移动硬盘。否则,就不是移动硬盘。
相关问题
QT中wmic diskdrive get serialnumber获得硬盘号的同时分别判断是否为移动硬盘
可以使用以下代码来实现:
```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;
}
```
这段代码可以获取所有硬盘的序列号,并判断它们是否为移动硬盘。
wmic diskdrive get serialnumber获得硬盘号的同时判断是否为移动硬盘
可以使用以下代码来实现:
```
@echo off
setlocal enabledelayedexpansion
for /f "skip=1 delims=" %%a in ('wmic diskdrive get serialnumber') do (
set "serial=%%a"
if not "!serial:~0,1!"==" " (
set "drive=!serial:~0,1!"
set "type=!serial:~4,1!"
if "!type!"=="5" (
echo Drive !drive! is a removable drive.
) else (
echo Drive !drive! is not a removable drive.
)
)
)
```
这段代码会遍历所有硬盘的序列号,并判断它们是否为移动硬盘。如果是移动硬盘,则输出相应的信息。如果不是移动硬盘,则输出相应的信息。
阅读全文