天天看點

Qt之複制檔案

bool QHGlobal::copyFile(QString sourcePath, QString dstPath, bool coverFile)
{
    if (sourcePath == dstPath)
    {
        return true;
    }

    dstPath.replace("\\", "/");

    if (!QFile::exists(sourcePath))
    {
        return false;
    }
    QDir *dir = new QDir;
    QString dstDir = dstPath.left(dstPath.lastIndexOf("/"));
    bool isExist = dir->exists(dstDir);
    if (isExist)
    {
        if (coverFile)
        {
            dir->remove(dstPath);
        }
    }
    else
    {
        dir->mkpath(dstDir);
    }

    if (!QFile::copy(sourcePath, dstPath))
    {
        delete dir;
        return false;
    }

    delete dir;
    return true;
}      

繼續閱讀