vb.net 删除所有文件和文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13243952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:02:15  来源:igfitidea点击:

Delete all files and folder

vb.netfile-io

提问by kelvzy

I want to delete all files and folders inside one folder.

我想删除一个文件夹中的所有文件和文件夹。

Code

代码

If Not Directory.Exists(txtTXT.Text) Then
     Return
End If

Dim files() As String
files = Directory.GetFileSystemEntries(txtTXT.Text)

For Each element As String In files
    If (Not Directory.Exists(element)) Then
        File.Delete(Path.Combine(txtTXT.Text, Path.GetFileName(element)))
    End If
Next

My code only deletes the files, but not the folders... How can I delete all?

我的代码只删除文件,但不删除文件夹......我怎样才能全部删除?

回答by kelvzy

I revised my program, so I used this code..

我修改了我的程序,所以我使用了这段代码..

My.Computer.FileSystem.DeleteDirectory( _
    My.Computer.FileSystem.SpecialDirectories.Desktop + "\epubcount", _
    FileIO.DeleteDirectoryOption.DeleteAllContents)

回答by paddy

Turn this code into a function. Make a recursive call to the function when you encounter a directory, passing the directory name to it. The function should also delete the directory that was passed in.

把这段代码变成一个函数。当您遇到一个目录时,对该函数进行递归调用,并将目录名称传递给它。该函数还应删除传入的目录。