bool QDir::removeRecursively()血的教训

5 篇文章 0 订阅

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();
}

老铁们,血的教训,长点心吧
在这里插入图片描述
如果你觉得这篇文章对你有帮助,请鼓励鼓励小编!!!蟹蟹

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值