复制文件到指定路径的代码模块
简单直接点
上代码模块,可以直接调用然后运行
//拷贝文件:
bool threadimportall::copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist)
{
toDir.replace("\","/");
qDebug()<<"sourceDir = "<<sourceDir << “toDir=”<<toDir;
if (sourceDir == toDir){
qDebug()<<"sourceDir sourceDir == toDi return ture";
return true;
}
if (!QFile::exists(sourceDir)){
qDebug()<<"sourceDir exists return false";
return false;
}
QFileInfo fi(toDir);
QString dirPath=fi.absolutePath();//取路径
QDir *createfile = new QDir;
bool exist=createfile->exists(dirPath);
if(!exist)
{
createfile->mkdir(dirPath);
}
exist = createfile->exists(toDir);
if (exist){
if(coverFileIfExist){
createfile->remove(toDir);
}
}
if(!QFile::copy(sourceDir, toDir))
{
qDebug()<<"sourceDir copy fail return false";
return false;
}
qDebug()<<"sourceDir copy return true";
return true;
}
调用很简单:
copyFileToPath(file_info.absoluteFilePath(),Path,true);
直接传入路径相关参数就可以
然后注意一下Path是QString类型的