Delete subfolders and files in vb.net

泄露秘密 提交于 2019-12-19 22:23:12

问题


Is it possible to delete all the subfolders (with content) and files within a folder?

For example:

  • Backup
    • November
      • pic1.jpg
      • pic2.jpg
    • December
    • January
      • pic3.jpg
    • example1.txt
    • example2.txt
    • example3.txt

There is a root folder (Backup). This root folder contains 3 subfolders (with content) and 3 text files. How can I delete the whole content (3 subfolders and 3 files) of the Backup folder without deleting the root folder (Backup) itself?


回答1:


The Directory class has a Delete method that accepts a parameter that forces the deleting operation recursively on the folder passed

' 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

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



来源:https://stackoverflow.com/questions/27272690/delete-subfolders-and-files-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!