递归删除当前文件夹及当前文件夹下的所有文件

递归删除当前文件夹及当前文件夹下的所有文件。

 

Imports System.IO Module Module1 Sub Main() DeleteFoldersAndFiles("C:/A") Console.ReadLine() End Sub Private Sub DeleteFoldersAndFiles(ByVal path As String) If path Is Nothing Then Return End If Dim dirs() As String = Directory.GetDirectories(path) Dim files() As String = Directory.GetFiles(path) Dim dirCount As Integer = dirs.Length Dim fileCount As Integer = files.Length If Not (dirCount = 0) Then If Not (fileCount = 0) Then DeleteFilesHelper(path) End If For i As Integer = 0 To dirCount - 1 DeleteFoldersAndFiles(dirs(i)) Next Else If Not (fileCount = 0) Then DeleteFilesHelper(path) DeleteDirectoryHelper(path) End If End If End Sub Private Sub DeleteFilesHelper(ByVal path As String) ' ' Stores all the files to a string array. ' Dim filesInCurrentDir() As String = Directory.GetFiles(path) ' Enumerates the files and deletes all the files under current folder. For i As Integer = 0 To Directory.GetFiles(path).Length - 1 File.Delete(filesInCurrentDir(i)) Next End Sub Private Sub DeleteDirectoryHelper(ByVal path As String) Directory.Delete(path) End Sub End Module

你可能感兴趣的:(递归删除当前文件夹及当前文件夹下的所有文件)