尝试在 VB.net 中创建提取程序

Trying to Create a Extractor program in VB.net

所以我正在尝试为一个游戏创建这个程序,它将以 .zip 形式从一个文件夹中获取模组并将它们全部解压缩到游戏文件夹中,这是我目前获得的代码。我认为 button2 的代码无法从另一个按钮获取文件夹我已经尝试了几种方法但无法让它工作。

Imports System.IO.Compression
Imports System.IO

Public Class Form1

Dim GameFolder As String
Dim ZipFolder As String


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Close()
End Sub

Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim mypath As String
    Dim fname As String

    mypath = ZipFolder  ' Select zip location folder
    fname = Dir(mypath & "*.zip")  ' get first zip file
    Do While Len(fname) > 0
        ZipFile.ExtractToDirectory(fname, mypath)
        fname = Dir()   ' get next matching file
    Loop
End Sub

Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim fd As FolderBrowserDialog = New FolderBrowserDialog() ' Let the user select the Zip Folder
    Dim ZipFolder As String
    If fd.ShowDialog() = DialogResult.OK Then
        ZipFolder = fd.SelectedPath 'Set the Zipfolder string to the output of the FolderBrowser
    End If
End Sub

Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim fd As FolderBrowserDialog = New FolderBrowserDialog() 'Let the user select the game folder
    Dim GameFolder As String
    If fd.ShowDialog() = DialogResult.OK Then
        GameFolder = fd.SelectedPath 'Set the GameFolder string to the output of the FolderBrowser
    End If
End Sub

End Class

除了代码顶部的初始声明,

删除所有其他

Dim GameFolder As String
Dim ZipFolder As String

原因是当您在 subs 中声明它们时,sub 中的任何代码都将优先使用局部声明而不是全局声明。

因此,当您将对话框结果分配给当前代码中的变量时,它们只会分配给局部变量,而不是表单范围变量。