• 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

  • 相关阅读:
    数组(2)
    数组(1)
    【探路者】第二周立会报告5(总第11次)
    【探路者】第二周立会报告4(总第10次)
    【探路者】第二周立会报告3(总第9次)
    【探路者】第二周立会报告2(总第8次)
    【探路者】第二周立会报告(总第7次)
    第五周作业1
    【探路者】10月19日立会报告(总第6次)
    【探路者】10月18日立会报告(团队第1周-第5次)
  • 原文地址:https://www.cnblogs.com/gethope5/p/15662175.html
Copyright © 2020-2023  润新知