Delete subfolders and files in vb.net

前端 未结 1 780
故里飘歌
故里飘歌 2021-01-13 07:01

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

For example:

  • Backup
    • November
      • pic1.jpg
1条回答
  •  迷失自我
    2021-01-13 07:47

    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.

    0 讨论(0)
提交回复
热议问题