win环境下获取利用qt获取u盘信息


最近有一个需求,需要在Windows环境下查找U盘,并下U盘中拷贝数据.遵照一般思路,先在网上搜寻各种资料,归纳起来有几种:

1,通过QStorageInfo,可以遍历所有的盘符,代码如下,可获得所有磁盘的盘符,

   QListcc= QStorageInfo::mountedVolumes();
   for(int i=0;i

参考资料

2,通过本地事件nativeEvent来实现,测试了下,不能发现u盘..

参考资料:https://blog.csdn.net/u011720560/article/details/80401857

3,通过QDir::drives(),获取所有驱动器,再用GetDriveType函数判断驱动器类型,实现U盘或普通磁盘的判别,,,但不能识别移动硬盘.修改后的代码如下:

    QFileInfoList list =  QDir::drives();  //获取当前系统的盘符
    int temp_number=0;
    std::vector driver_types;
    for(int i=0;ii)
    {
        UINT driver_type = GetDriveType((WCHAR *) list[i].filePath().utf16());
        driver_types.push_back(driver_type);
        switch (driver_type) {
        case 0:
            //qDebug()<
            break;
        case 1:
            //qDebug()<
            break;
        case 2:
            temp_number++;
            qDebug()<<"u disk"<<list.at(i).absolutePath();
            //qDebug()<
            break;
        case 3:
            //qDebug()<
            qDebug()<<"normal disk"<<list.at(i).absolutePath();;
            break;
        case 4:
            //qDebug()<
            break;
        case 5:
            //qDebug()<
            break;
        case 6:
            //qDebug()<
            break;
        default:
            break;
        }
    }

参考资料:https://blog.csdn.net/mars_xiaolei/article/details/103146372

QT