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

时间:2014-12-03 13:14:57

标签: vb.net file-io delete-directory

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

例如:

  • 备份
    • 十一月
      • pic1.jpg
      • pic2.jpg
    • 一月
      • pic3.jpg
    • example1.txt
    • example2.txt
    • example3.txt

有一个根文件夹(备份)。此根文件夹包含3个子文件夹(包含内容)和3个文本文件。如何在不删除根文件夹(备份)本身的情况下删除备份文件夹的全部内容(3个子文件夹和3个文件)?

1 个答案:

答案 0 :(得分:8)

Directory类有一个删除方法,该方法接受一个参数,该参数在传递的文件夹上递归执行删除操作

' 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

  

删除指定的目录,如果指示,则删除所有子目录   和目录中的文件。