QT 删除文件指定目录
1 bool deleteDir(const QString &dirName) 2 { 3 QDir directory(dirName); 4 if (!directory.exists()) 5 { 6 return true; 7 } 8 9 10 QString srcPath = QDir::toNativeSeparators(dirName); 11 if (!srcPath.endsWith(QDir::separator())) 12 srcPath += QDir::separator(); 13 14 15 QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden); 16 bool error = false; 17 for (QStringList::size_type i=0; i != fileNames.size(); ++i) 18 { 19 QString filePath = srcPath + fileNames.at(i); 20 QFileInfo fileInfo(filePath); 21 if (fileInfo.isFile() || fileInfo.isSymLink()) 22 { 23 QFile::setPermissions(filePath, QFile::WriteOwner); 24 if (!QFile::remove(filePath)) 25 { 26 qDebug() << "remove file" << filePath << " faild!"; 27 error = true; 28 } 29 } 30 else if (fileInfo.isDir()) 31 { 32 if (!deleteDir(filePath)) 33 { 34 error = true; 35 } 36 } 37 } 38 39 40 if (!directory.rmdir(QDir::toNativeSeparators(directory.path()))) 41 { 42 qDebug() << "remove dir" << directory.path() << " faild!"; 43 error = true; 44 } 45 return !error; 46 }