vb.net 删除文件夹和包含文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2241673/
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-09 14:40:47  来源:igfitidea点击:

Delete Folders and Containing Files

vb.netvisual-studio-2010directorydelete-file

提问by lab12

I have a really quick question. My program actually downloads a zip file then extracts it onto their desktop. But I need an uninstall feature for it, which is basically deleting multiple folders and containing files. How can I do this in vb.net?

我有一个非常简单的问题。我的程序实际上下载了一个 zip 文件,然后将其解压缩到他们的桌面上。但我需要一个卸载功能,它基本上是删除多个文件夹和包含文件。我怎样才能在 vb.net 中做到这一点?

回答by Steve Danner

If all of your folders are contained in a single folder, it should be pretty straight forward.

如果您的所有文件夹都包含在一个文件夹中,则应该非常简单。

Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YOURPATH"
System.IO.Directory.Delete(path, True)

That will delete your root directory, and all the directories and files below it. You could just call this several times over if your files and directories are not all in a single root directory like "YOURPATH" in the example. This will spare you from having to remove each file individually.

这将删除您的根目录及其下面的所有目录和文件。如果您的文件和目录不在示例中的“YOURPATH”这样的单个根目录中,您可以多次调用它。这将使您不必单独删除每个文件。

回答by Mark Brittingham

The .NET IO unit has a two commands that should let you do the trick:

.NET IO 单元有两个命令可以让你做到这一点:

System.IO.Directory.GetDirectories("C:\Program Files\Your Directory\*.*");
System.IO.Directory.GetFiles("C:\Program Files\Your Directory\*.*");

I would write a method that takes the name of a directory and uses the "GetFiles" routine to get all of the files and to delete them using System.IO.File.Delete(path) in a foreach loop. Then, run a foreach loop on the result of the GetDirectories() command calling the function recursively.

我会编写一个方法,该方法采用目录名称并使用“GetFiles”例程来获取所有文件,并在 foreach 循环中使用 System.IO.File.Delete(path) 删除它们。然后,对递归调用函数的 GetDirectories() 命令的结果运行 foreach 循环。

Update: Steve Danner points out that the System.IO.Directory namespace has a Delete method so you don't need to go through the loops I talk about here. His answer is the right one and should be voted up.Mine, at this point, is more of a curiosity (although thank you to the person who gave me an upvote ;0).

更新:Steve Danner 指出 System.IO.Directory 命名空间有一个 Delete 方法,所以你不需要经历我在这里谈论的循环。 他的答案是正确的,应该投赞成票。在这一点上,我的更多是好奇心(尽管感谢给我点赞的人;0)。

回答by MarianoC

Your are looking for DirectoryInfo, use it like this:

你正在寻找 DirectoryInfo,像这样使用它:

Dim di As New IO.DirectoryInfo(path)
di.Delete(True)

回答by Emann

Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YOURPATH"
System.IO.Directory.Delete(path, True)