2014-12-03 90 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

相關問題