c++ - 为什么删除所有内容后不删除父目录?
问题描述
我想通过 Qt 功能实现一个功能,该功能类似于QDir::removeRecursively()
并删除一个目录,包括其文件和子目录。问题是该函数成功删除了所有包含文件,即使在子目录中,但不会删除父目录。
void removeDirectory(const QString &path)
{
QDir directory(path);
if(!directory.exists())
throw QString("The directory does not exist.");
QFileInfoList entries = directory.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries);
for(QFileInfoList::const_iterator counter = entries.cbegin(); counter != entries.cend(); ++counter)
{
if(counter->isDir())
removeDirectory(counter->absoluteFilePath());
else
{
if(!QFile::remove(counter->absoluteFilePath()))
throw QString(counter->absoluteFilePath() + " is not removed.");
else
qDebug() << counter->absoluteFilePath() << " is removed.";
}
}
if(directory.rmdir("."))
qDebug() << directory.absolutePath() << " is removed.";
else
qDebug() << directory.absolutePath() << " is failed to remove.";
}
我想问题出directory.rmdir(".")
在我告诉程序删除当前目录的地方,但是这个操作失败了,因此目录本身没有被删除,尽管它是空的。
那么,为什么会发生这种情况,以及如何解决这个问题?
解决方案
推荐阅读
- internet-explorer-11 - 使用 UIAutomation 无法识别 IE11 Saveas 框架通知栏子项
- php - Laravel Route 资源不删除表数据?
- ios - 失踪的奥特莱斯
- android - 从移动设备中隐藏 Chrome URL 窗口
- ffmpeg - FFMPEG h264_videotoolbox 输出很奇怪
- c# - 为什么 `.Select(...).Last()` 被优化,但 `.Select(...).Last(...)` 没有?
- python-3.x - 如何在opencv python中为网络摄像头视频提供透明边框?
- hyperledger-fabric - 在 Hyperledger Fabric 中按块号定位账本文件名
- javascript - Angular 如何保存数据
- python - Tkinter 从 PopUp 读取输入值