简体   繁体   English

VB.net选择文件夹中的文件和文件夹

[英]VB.net Selecting files and folders in a folder

I'm trying to select the files and folders inside a folder to zip up, but what it seems to be doing is selecting all the folders up to the folder i've selected and the files in the final folder but not the folders in there and zipping them up. 我正在尝试选择要压缩的文件夹中的文件和文件夹,但是似乎在做的是选择所有文件夹,直到我选择的文件夹,然后选择最终文件夹中的文件,而不是其中的文件夹并拉上拉链。 So for example in tbFolder I have the string: "C:\\Users\\tomb\\Desktop\\DeOld\\Mota7" I want to select this folder and zip up the entire contents of this folder, images and all. 因此,例如在tbFolder中,我具有字符串:“ C:\\ Users \\ tomb \\ Desktop \\ DeOld \\ Mota7”,我想选择此文件夹并压缩该文件夹,图像和所有内容的全部内容。 But whats happening is the following: 但是发生了以下情况:

在此处输入图片说明

Its creating the folder structure up to the folder i want zipped and adding just the files in side this and not the folders. 它创建了要压缩的文件夹的文件夹结构,并仅添加了该文件夹中的文件,而不是文件夹。 I'm guessing I need to tweak the line: 我猜我需要调整线:

System.IO.Directory.GetFiles(DirectoryToZip)

Here is the entire code: 这是完整的代码:

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

        Dim ZipFile As String = "C:\Releases\" & drpService.Text & "-" & DateTime.Now.ToString("YYmmDD") & ".zip"
        Dim DirectoryToZip As String = tbFolder.Text
        Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)

        Using zip As ZipFile = New ZipFile
            zip.AddFiles(filenames)
            zip.Save(ZipFile)
        End Using


    End Sub

如果您使用的是DotNetZip ,则他们的站点上有一个示例可以添加整个目录

zip.AddDirectory(DirectoryToZip, "RootFolderInZip");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM