遍历目录
删除
/*
* 删除文件夹
*/
static bool deleteDirectory(const QString &path)
{
if (path.isEmpty())
return false;
QDir dir(path);
if(!dir.exists())
return true;
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
QFileInfoList fileList = dir.entryInfoList();
foreach (QFileInfo fi, fileList)
{
if (fi.isFile())
fi.dir().remove(fi.fileName());
else
deleteDirectory(fi.absoluteFilePath());
}
return dir.rmpath(dir.absolutePath());
}
拷贝
/*
* 拷贝文件夹
*/
static bool copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
QDir targetDir(toDir);
if(!targetDir.exists())
{ /* 如果目标目录不存在,则进行创建 */
if(!targetDir.mkdir(targetDir.absolutePath()))
return false;
}
QFileInfoList fileInfoList = sourceDir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList){ /* 遍历源文件夹内所有文件 */
if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
continue;
if(fileInfo.isDir()){ /* 当为目录时,递归的进行copy */
if(!copyDirectoryFiles(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()),
coverFileIfExist))
return false;
}
else{ /* 当允许覆盖操作时,将旧文件进行删除操作 */
if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){
targetDir.remove(fileInfo.fileName());
}
// 进行文件copy
if(!QFile::copy(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()))){
return false;
}
}
}
return true;
}
获得当前路径
// 获取当前进程的全路径
qApp->applicationFilePath();
// 获得当前程序, 所在目录
qDebug()<< "current applicationDirPath: " << QCoreApplication::applicationDirPath();
// 获得当前工作路径
// 一般启动时, 为程序所在目录的上层
qDebug()<< "current currentPath: " << QDir::currentPath();
拼接路径