2014-12-03 92 views
3

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

例如:

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

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

回答

7

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 

FROM MSDN Directory.Delete

的文件夹的删除操作的参数删除指定目录以及(如果有的话)任何子目录 和目录中的文件。

+1

此外,对于每个DirectoryFile作为字符串在Directory.GetFiles(“c:\ backup”)File.Delete(DirectoryFile)下一步删除文件以及。 – Capellan 2014-12-03 13:36:46

+1

是的,缺少那一点 – Steve 2014-12-03 13:40:24

相关问题