如何在Qt中检查所选驱动器的可用空间?

问题描述 投票:0回答:1

我在Windows 7中使用Qt5.6.2。我的目标是将CustomData文件保存到选定的驱动器(主要是Pen驱动器或本地驱动器或网络驱动器)。

因此,在保存文件之前,我想检查可用内存并写入所选路径的访问权限。

从这个thread我开始了解QStorageInfo类。所以我写了下面的函数。

bool ImportExport::checkWriteAccessAndEnoughDriveSpace(const QString &path,QString &error)
{
   error = QString();
   int fileSizeMB = 100;//for testing purpose I am comparing with 100 MB. correct solution is to compare with size of the file.
   qDebug() << "path to QStorage: " <<path;
   QStorageInfo storage(path);
   //QStorageInfo storage = QStorageInfo::root();
   qDebug() << "export root path: " <<storage.rootPath();
   qDebug() << "volume name:" << storage.name();
   qDebug() << "fileSystemType:" << storage.fileSystemType();
   qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB";
   qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB";

   if (storage.isValid() && storage.isReady()) {
      if (!storage.isReadOnly()) {
         // check enough memory
         int MBavailable = storage.bytesAvailable()/1000/1000;
         if(MBavailable > fileSizeMB){
            return true;
         }else{
            error = tr("Not enough disk space, available disk space is only : ") + QString::number(MBavailable);
            return false;
         }
      }else{
         error = tr("No permission to write to current folder ");
         qDebug() << error; // how to set this message as toolTip on the fileDialog
         return false;
      }
   }else{
      error = tr("Selected drive validity: ")+ QString::number(storage.isValid()) +tr("or storage availability: ") +QString::number(storage.isReady());
      qDebug() << error; // how to set this message as toolTip
      return false;
   }
}

这是调试输出:

path to QStorage:  "D:/Aluminium Demo DMU105mB.cba"
export root path:  ""
volume name: ""
fileSystemType: ""
size: 0 MB
availableSize: 0 MB
"Selected drive validity: 0or storage availability: 0"

正如您所看到的,QStorageInfo总是给我0大小,驱动器没准备好。无论我选择哪种驱动器,结果总是一样的。任何人都可以指出错误吗?我问题的任何解决方案?先感谢您。

编辑1:如果我硬编码存储构造函数

QStorageInfo存储(“D:”);然后一切正常。如果我给路径QStorageInfo存储(“D:\ Application Cube DMU65mB.cba”);

然后它不起作用。用户可以选择任何驱动器(本地驱动器或可移动驱动器)。在选择文件夹时,我从QDialog获取路径(此处未显示代码)。所以我想如果我只能从路径获得Drive信息,那么我的问题就解决了。现在,我怎样才能获得驱动器名称?我可以通过解析我从QDialog获得的路径,但任何更好的解决方案?谢谢

编辑2:我已将构造函数更改为QStorageInfo存储(路径);。然后它适用于本地驱动器,但如果用户选择可移动驱动器(笔式驱动器),则它不起作用。任何人都知道为什么它不适用于Pen Drive?谢谢。

c++ qt memory qfile qfiledialog
1个回答
0
投票

问题在于给予QStorageInfo类的构造函数的路径。如果路径包含文件名,那么它将无法工作。以下作品:

   QFileInfo info(fullPath);
   QString path = info.path();
   QStorageInfo storage(path);

注意:Fullpath包含文件名,而路径不包含。

然后是下一个问题,即使你有写访问权限,QStorageInfo::isReadOnly()也会给你错误。由于Windows-NFTS文件系统,这不起作用。所以诀窍是检查你是否可以sucecssfully创建一个临时文件。

  if (storage.isValid() && storage.isReady()) {
      //check write permission
      QString filename(path+"dummyTempJob.job"); //temp file, a workaround to check write access because QFileInfo isWritable and storage.isReadOnly() doesn't work for windows NTFS system
      QFile file(filename);
      if(file.open(QIODevice::WriteOnly)){
          file.remove();//delete the temp file
          checkWriteAccess = true;
      }else{
         error = QObject::tr("No write permissions to %1.").arg(path);
         qCritical()<< error;
      }
   }else{
      error = QObject::tr("Drive %1 is not available.").arg(storage.rootPath());
      qCritical() << error << QStringLiteral(" valid = %1, ready = %2").arg(storage.isValid()).arg(storage.isReady());
   }
© www.soinside.com 2019 - 2024. All rights reserved.