在Win32中删除子文件夹之前,如何从文件夹和子文件夹中删除文件? [英] How can I delete files from folders and subfolders before deleting the subfolders themselves in Win32?

查看:189
本文介绍了在Win32中删除子文件夹之前,如何从文件夹和子文件夹中删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除Win32 API(RemoveDirectory)中的目录要求删除该文件夹下的所有文件,然后再删除文件夹。因此,假设我在根目录下有一个文件夹foo,其中有文件,还有一些子文件夹,也可能有或没有文件,如何递归删除所有文件,然后是子文件夹?

解决方案

它通常需要一个递归函数,读取要删除的目录的内容(通过 FindFirstFile / code> / FindNextFile()) - 如果遇到文件,请将其删除,否则递归到文件夹,然后在递归返回时删除文件夹。 / p>

或者您可以使用 IFileOperation ,系统将为您完成所有工作。


Deleting a directory in Win32 API (RemoveDirectory) requires all files under that folder to be deleted before deleting the folder. So assuming I have a folder 'foo' at the root, with files in it, along with some subfolders that also may or may not have files, how do I recursively delete all the files, and then the subfolders?

解决方案

It generally requires a recursive function, that reads the contents of the directory you want to delete (via FindFirstFile()/FindNextFile()) - if a file is encountered, delete it, otherwise recurse into the folder and then delete the folder when the recursion returns.

Or you can just use IFileOperation and the system will do all the work for you.

这篇关于在Win32中删除子文件夹之前,如何从文件夹和子文件夹中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆