Qt库中的 bool QDir::removeRecursively()这个方法要谨慎使用,如果使用不当就会删除项目中的所有文件
官方文档中这样写:
If a file or directory cannot be removed, removeRecursively() keeps going and attempts to delete as many files and sub-directories as possible, then returns false.
Note: this function is meant for removing a small application-internal directory (such as a temporary directory), but not user-visible directories. For user-visible operations, it is rather recommended to report errors more precisely to the user, to offer solutions in case of errors, to show progress during the deletion since it could take several minutes, etc.
#include<QDir>
#include<QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtWidgetsApplication2 w;
QDir dir;
QString deletePath = QDir::currentPath() + "/bin/";
qDebug() << deletePath;
dir.setPath(deletePath); // 务必指定路径,不然会从工程文件开始删,后果不堪设想
dir.makeAbsolute();
if (dir.exists(deletePath))
{
qDebug() << "存在";
dir.removeRecursively(); // 如果存在删除整个bin路径
}
w.show();
return a.exec();
}
老铁们,血的教训,长点心吧
如果你觉得这篇文章对你有帮助,请鼓励鼓励小编!!!蟹蟹