删除vb.net中的子文件夹和文件

是否可以删除文件夹中的所有子文件夹(包含内容)和文件?

例如:

>备份

>十一月

> pic1.jpg
> pic2.jpg

>十二月
>一月

> pic3.jpg

> example1.txt
> example2.txt
> example3.txt

有一个根文件夹(备份).此根文件夹包含3个子文件夹(包含内容)和3个文本文件.如何删除备份文件夹的整个内容(3个子文件夹和3个文件)而不删除根文件夹(备份)本身?

解决方法

Directory类有一个Delete方法,该方法接受一个参数,该参数以递归方式强制对传递的文件夹执行删除操作

' Loop over the subdirectories and remove them with their contents
For Each d in Directory.GetDirectories("C:\Backup")
    Directory.Delete(d,true)
Next

' Finish removing also the files in the root folder
For Each f In Directory.GetFiles("c:\backup") 
     File.Delete(f) 
Next

来自MSDN Directory.Delete

Deletes the specified directory and,if indicated,any subdirectories and files in the directory.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐