其他分享
首页 > 其他分享> > win环境下获取利用qt获取u盘信息

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

作者:互联网

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

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

   QList<QStorageInfo>cc= QStorageInfo::mountedVolumes();
   for(int i=0;i<cc.count();i++)
   {
       QStorageInfo storage = cc.at(i);
       if (storage.isReadOnly())
           qDebug() << "isReadOnly:" << storage.isReadOnly();
       qDebug() << storage.rootPath()
                << "name:" << storage.name()
                << "fileSystemType:" << storage.fileSystemType()
                << "size:" << storage.bytesTotal()/1000/1000 << "MB"
                << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB";
   }

参考资料

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<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()<<"u disk"<<list.at(i).absolutePath();
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" 可移动驱动器:软盘驱动器,拇指驱动器或闪存卡读取器";
            break;
        case 3:
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" 固定驱动器:硬盘驱动器或闪存驱动器";
            qDebug()<<"normal disk"<<list.at(i).absolutePath();;
            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;
        }
    }

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

标签:qt,int,win,盘符,获取,QDir,参考资料,U盘
来源: https://www.cnblogs.com/gethope5/p/15662175.html