我是Qt新手,我需要幫助將本地計算機的特定路徑中的所有文件傳輸到外部USB驅動器。C++從Qt傳輸文件到外部USB驅動器
回答
我已經解決了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中,我給了文件的靜態名稱。然後,我只是從本地機器的文件中複製數據到我在USB中創建的文件中,在QFile::copy(srcPath, dstPath)
的幫助下。我希望它能幫助別人。
複製單個文件
您可以使用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
方法,它很可能存在(非常可能,因爲它不是原子操作,目錄或文件可能會在對話框和對話框之間被刪除或重命名複製操作,但這是一個不同的問題)。
感謝您的幫助,我會用它來解決我的問題。 –
很高興幫助,讓我知道它是否解決了您的問題,如果您需要進一步的幫助 – cbuchart
Idk我仍然停留在這個問題上。我有文件以及外部驅動器的靜態路徑,如** QString srcPath = dir.relativeFilePath(「/ home/untitled」); **。我沒有使用[QFileDialog](http://doc.qt.io/qt-5/qfiledialog。HTML)因爲靜態的東西。我只是在頭文件中聲明源文件和目標文件夾,並在main.cpp中調用** QFile :: copy(srcPath,dstPath); **。 –
- 1. USB數據傳輸到外部USB 3
- 2. 如何阻止USB以阻止筆驅動器和外部硬盤驅動器
- 3. MediaStore.Audio.Media.DATA爲空的USB驅動器文件
- 4. 當從usb傳輸項目到c:\
- 5. iPad - 來自外部驅動器的html文件輸入
- 6. 批處理文件到基於驅動器名稱的USB驅動器
- 7. 識別內部驅動器vs USB驅動器bash
- 8. 從二進制文件創建可啓動的USB驅動器?
- 9. CSS問題(將代碼從內部傳輸到外部文件)
- 10. SQL Server會將USB 3.0外部驅動器視爲本地?
- 11. Visual Studio 2010:無法從外部驅動器打開文件
- 12. C# - 在Windows XP上未檢測到事件--USB驅動器
- 13. C++ MFC寫的文件,插入USB驅動器
- 14. 將HDFS文件映射到外部驅動器時出錯
- 15. 來自c的USB筆驅動器identifer#
- 16. C#檢查存在的USB驅動器
- 17. 在C++中卸載USB驅動器
- 18. 從C驅動器讀取XML文件
- 19. 如何將文件流式傳輸到硬盤驅動器?
- 20. 枚舉外部驅動器
- 21. MongoDB在外部驅動器
- 22. 使用C#將文件從本地驅動器複製到共享驅動器
- 23. 上傳文件的一部分到谷歌驅動器
- 24. 如何在android中從usb驅動器讀取文件?
- 25. .bat文件打開特定文件夾並從USB驅動器複製文件
- 26. 將所有驅動器上的文件/文件夾複製到外部驅動器?
- 27. 從USB驅動器運行程序
- 28. 如何從usb驅動器運行nodejs?
- 29. 從USB閃存驅動器運行Tomcat
- 30. bash腳本查找/從USB驅動器
您可能會發現這些鏈接很有用:http://doc.qt.io/qt-4.8/qfiledialog.html和http://stackoverflow.com/questions/19928216/qt-copy-a-file-from-一個目錄到另一個目錄 – ni1ight
請[編輯]你的問題以顯示[迄今爲止的代碼](http://whathaveyoutried.com)。你至少應該包括一個你遇到問題的代碼大綱(但最好是[mcve]),然後我們可以嘗試幫助解決具體問題。你還應該閱讀[問]。 –