加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:23:12 所属栏目:百科 来源:网络整理
导读:是否可以删除文件夹中的所有子文件夹(包含内容)和文件? 例如: 备份 十一月 pic1.jpg pic2.jpg 十二月 一月 pic3.jpg example1.txt example2.txt example3.txt 有一个根文件夹(备份).此根文件夹包含3个子文件夹(包含内容)和3个文本文件.如何删除备份文件夹
是否可以删除文件夹中的所有子文件夹(包含内容)和文件?

例如:

>备份

>十一月

> 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读