仅当目录为空时,如何正确删除该目录?
我想删除一个目录当且仅当它不包含文件时。
我目前正在检查目录中是否有 0 个文件系统条目,然后立即删除,但这留下了我想避免的边缘情况。像锁定目录之类的东西似乎就是这样。大多数文件系统可能不提供此功能,但也许有一些解决方法?
I would like to delete a directory if and only if it contains no files.
I'm currently checking for 0 file system entries within the directory then deleting right away, but that leaves an edge case that I would like to avoid. Something like locking the directory seems to be the way. Most filesystems probably don't offer this feature, but maybe there is some workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果目录不为空 Directory.Delete 不会删除它并抛出 IOException
if directory is not empty Directory.Delete doesn't deletes it and throws IOException
当您使用
Directory.Delete(String)
它将抛出IOException
当您指定的目录不为空。When you use
Directory.Delete(String)
it will throw anIOException
when your specified directory is not empty.