删除文件夹和包含的文件

发布于 2024-08-20 15:18:12 字数 98 浏览 8 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微暖i 2024-08-27 15:18:12

如果所有文件夹都包含在一个文件夹中,那么它应该非常简单。

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

这将删除您的根目录及其下面的所有目录和文件。如果您的文件和目录不全部位于单个根目录(如示例中的“YOURPATH”)中,您可以多次调用此命令。这将使您不必单独删除每个文件。

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.

抚笙 2024-08-27 15:18:12

.NET IO 单元有两个命令可以让您实现这一目的:

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

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

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

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

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.

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).

万水千山粽是情ミ 2024-08-27 15:18:12

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

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

Your are looking for DirectoryInfo, use it like this:

Dim di As New IO.DirectoryInfo(path)
di.Delete(True)
柏林苍穹下 2024-08-27 15:18:12
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YOURPATH"
System.IO.Directory.Delete(path, True)
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\YOURPATH"
System.IO.Directory.Delete(path, True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文