QDir递归删除文件夹(包括该文件夹中的文件)的简单方法

比如删除一些临时目录,该文件夹中的文件又不多的时候,该函数就派上用场了:
实例代码:

QDir dir("目标文件夹名");
dir.removeRecursively();

官方文档这样说:

bool QDir::removeRecursively()

Removes the directory, including all its contents.
Returns true if successful, otherwise false.
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.
If the directory was already removed, the method returns true (expected result already reached).
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.
This function was introduced in Qt 5.0.

翻译如下:

该函数是Qt 5.0开始引入的。

如果成功,则返回true;失败返回false
如果一个文件或者目录不能被删除,removeRecursively()将会继续执行并试图删除其他文件和子目录,直到删除掉该目录下能删除的文件和目录后,才会返回false,表示失败。

如果目录已被删除,则该方法将返回true(已达到预期结果)。

注意:此函数用于删除小的应用程序内部目录(如临时目录),但不用于用户可见的目录。对于用户可见的操作,建议更精确地向用户报告错误,在出现错误时提供解决方案,显示删除过程中的进度,因为这可能需要几分钟,等等。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值