簡體   English   中英

C ++將文件從Qt傳輸到外部USB驅動器

[英]C++ Transfer Files From Qt to External USB Drive

我是Qt的新手,我需要幫助將所有文件從本地計算機的特定路徑傳輸到外部USB驅動器。

復制單個文件

您可以使用QFile::copy

QFile::copy(srcPath, dstPath);

注意:此功能不會覆蓋文件,因此,如果存在以前的文件,則必須刪除它們:

if (QFile::exist(dstPath)) QFile::remove(dstPath);

如果您需要顯示一個用戶界面來獲取源路徑和目標路徑,則可以使用QFileDialog的方法來實現。 例:

bool copyFiles() {
  const QString srcPath = QFileDialog::getOpenFileName(this, "Source file", "",
    "All files (*.*)");
  if (srcPath.isNull()) return false; // QFileDialog dialogs return null if user canceled

  const QString dstPath = QFileDialog::getSaveFileName(this, "Destination file", "",
    "All files (*.*)"); // it asks the user for overwriting existing files
  if (dstPath.isNull()) return false;

  if (QFile::exist(dstPath))
    if (!QFile::remove(dstPath)) return false; // couldn't delete file
      // probably write-protected or insufficient privileges

  return QFile::copy(srcPath, dstPath);
}

復制目錄的全部內容

我將答案擴展到srcPath是目錄的情況。 必須手動並遞歸完成。 這是執行此操作的代碼,無需進行錯誤檢查即可簡化操作。 您必須負責選擇正確的方法(有關一些想法,請QFileInfo::isFile

void recursiveCopy(const QString& srcPath, const QString& dstPath) {
  QDir().mkpath(dstPath); // be sure path exists

  const QDir srcDir(srcPath);
  Q_FOREACH (const auto& dirName, srcDir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) {
    recursiveCopy(srcPath + "/" + dirName, dstPath + "/" + dirName);
  }

  Q_FOREACH (const auto& fileName, srcDir.entryList(QStringList(), QDir::Files, QDir::Name)) {
    QFile::copy(srcPath + "/" + fileName, dstPath + "/" + fileName);
  }
}

如果需要詢問目錄,可以使用QFileDialog::getExistingDirectory

結束語

兩種方法都假定srcPath存在。 如果使用QFileDialog方法,則很有可能存在它(很有可能,因為它不是原子操作,並且對話框或復制操作之間的目錄或文件可能會被刪除或重命名,但這是一個不同的問題)。

我已經解決了QStorageInfo::mountedVolumes()的問題,該問題返回了連接到計算機的設備列表。 但是除了Pendrive或HDD以外,其他所有人都沒有名字。 因此(!(storage.name()).isEmpty()))它只會將路徑返回到那些設備。

QString location;
QString path1= "/Report/1.txt";
QString locationoffolder="/Report";

foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
    if (storage.isValid() && storage.isReady() && (!(storage.name()).isEmpty())) {
        if (!storage.isReadOnly()) {

           qDebug() << "path:" << storage.rootPath();
           //WILL CREATE A FILE IN A BUILD FOLDER
           location = storage.rootPath();
           QString srcPath = "writable.txt";
           //PATH OF THE FOLDER IN PENDRIVE
           QString destPath = location+path1;
           QString folderdir = location+locationoffolder;
           //IF FOLDER IS NOT IN PENDRIVE THEN IT WILL CREATE A FOLDER NAME REPORT
           QDir dir(folderdir);
           if(!dir.exists()){
              dir.mkpath(".");
           }

           qDebug() << "Usbpath:" <<destPath;
           if (QFile::exists(destPath)) QFile::remove(destPath);
           QFile::copy(srcPath,destPath);
           qDebug("copied");

        }
    }
}

由於需要,我必須創建一個文件夾以及USB文件夾,並且為文件指定了一個靜態名稱。 然后,我只是借助QFile::copy(srcPath, dstPath)將數據從本地計算機的文件復制到了我在USB中創建的文件。 希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM