1、先检测驱动器;
2、可以使用GetDriveType函数来获取驱动器的类型,以下是函数的链接;
3、通过数量变化来判断插入或者移除。
关键代码
/*******************************************************
* @brief -
* @author xiaolei
* @copyright -
* @version V1.0
* @data 2019-11-19
* @note 1、检测盘符
* @note 2、判断是否插入/移除移动驱动器
* @note 3、没有获取设备ID,因此不知道删除的是哪个设备?
* @note -
*******************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFileInfoList list = QDir::drives(); //获取当前系统的盘符
qDebug()<<"驱动器数量: "<<list.count();
driver_number=list.count();
for(int i=0;i<list.count();++i)
{
UINT driver_type = GetDriveType((WCHAR *) list[i].filePath().utf16());
switch (driver_type) {
case 0:
qDebug()<<list[i].filePath()<<" "<<driver_type<<" 驱动类型不能确定";
break;
case 1:
qDebug()<<list[i].filePath()<<" "<<driver_type<<" 根路径无效";
break;
case 2:
qDebug()<<list[i].filePath()<<" "<<driver_type<<" 可移动驱动器:软盘驱动器,拇指驱动器或闪存卡读取器";
removable_number++;
break;
case 3:
qDebug()<<list[i].filePath()<<" "<<driver_type<<" 固定驱动器:硬盘驱动器或闪存驱动器";
break;
case 4:
qDebug()<<list[i].filePath()<<" "<<driver_type<<" 远程(网络)驱动器";
break;
case 5:
qDebug()<<list[i].filePath()<<" "<<driver_type<<" CD-ROM驱动器";
break;
case 6:
qDebug()<<list[i].filePath()<<" "<<driver_type<<" RAM磁盘";
break;
default:
break;
}
}
timer=new QTimer(this);
timer->start(3000);
connect(timer,&QTimer::timeout,this,&MainWindow::detectDriver);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::detectDriver()
{
QFileInfoList list = QDir::drives(); //获取当前系统的盘符
int temp_number=0;
std::vector<UINT> driver_types;
for(int i=0;i<list.count();++i)
{
UINT driver_type = GetDriveType((WCHAR *) list[i].filePath().utf16());
driver_types.push_back(driver_type);
switch (driver_type) {
case 0:
//qDebug()<<list[i].filePath()<<" "<<driver_type<<" 驱动类型不能确定";
break;
case 1:
//qDebug()<<list[i].filePath()<<" "<<driver_type<<" 根路径无效";
break;
case 2:
temp_number++;
//qDebug()<<list[i].filePath()<<" "<<driver_type<<" 可移动驱动器:软盘驱动器,拇指驱动器或闪存卡读取器";
break;
case 3:
//qDebug()<<list[i].filePath()<<" "<<driver_type<<" 固定驱动器:硬盘驱动器或闪存驱动器";
break;
case 4:
//qDebug()<<list[i].filePath()<<" "<<driver_type<<" 远程(网络)驱动器";
break;
case 5:
//qDebug()<<list[i].filePath()<<" "<<driver_type<<" CD-ROM驱动器";
break;
case 6:
//qDebug()<<list[i].filePath()<<" "<<driver_type<<" RAM磁盘";
break;
default:
break;
}
}
if(temp_number>removable_number)
{
qDebug()<<"驱动器数量: "<<list.count();
qDebug()<<"插入可移动驱动器!!!";
}
else if(temp_number==removable_number)
{
//qDebug()<<"未插入/移除可移动驱动器!!!";
}
else if(temp_number<removable_number)
{
qDebug()<<"驱动器数量: "<<list.count();
qDebug()<<"移除可移动驱动器!!!";
}
removable_number=temp_number;
driver_number=list.count();
}