您的位置:首页 > 其它

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

2009-11-03 19:49 316 查看
递归删除当前文件夹及当前文件夹下的所有文件。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: