我想删除文件夹中包含的所有文件。我使用的代码删除根文件夹中的所有文件,但不会删除子文件夹中的文件。这是代码:
If Not Directory.Exists("C:\New Folder") Then
Return
End If
Dim files() As String
files = Directory.GetFileSystemEntries("C:\New Folder")
For Each element As String In files
If (Not Directory.Exists(element)) Then
File.Delete(Path.Combine("C:\New Folder", Path.GetFileName(element)))
End If
Next
我想要的是:
I want to delete all the files inside the folder “New Folder”. At the same time, I want to keep the sub folders and delete all the files it contains. So, after the operation, “New Folder” may have any number of sub folders but it should not have even a single file.
最佳答案
尝试这个递归子
Sub DeleteFilesFromFolder(Folder As String)
If Directory.Exists(Folder) Then
For Each _file As String In Directory.GetFiles(Folder)
File.Delete(_file)
Next
For Each _folder As String In Directory.GetDirectories(Folder)
DeleteFilesFromFolder(_folder)
Next
End If
End Sub
'Somewhere you call
DeleteFilesFromFolder("C:\New Folder")
关于vb.net - 如何删除文件夹中的所有文件(包括子文件夹中的文件)而不删除文件夹本身或其任何子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221181/