【问题标题】:VB.NET - Access to path %appdata% is deniedVB.NET - 访问路径 %appdata% 被拒绝
【发布时间】:2014-06-29 15:11:50
【问题描述】:

当我遇到这个问题时,我正在为 Minecraft 社区制作一个 mod 安装程序:

这是我的代码:

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

            Button1.Enabled = False
            Button2.Enabled = False
            ComboBox1.Enabled = False
            Button1.Text = "DOWNLOADING... DO NOT QUIT!"



    Dim selected As String
    Dim issel As Boolean
    issel = False
    selected = ComboBox1.SelectedItem
    If selected = "Minecade Mod 1.7.2" Then
        selected = "5"
        issel = True
    End If
    If selected = "Minecade Mod 1.7.2 with OptiFine Standard" Then
        selected = "3"
        issel = True
    End If
    If selected = "Minecade Mod 1.7.2 with Optifine Ultra" Then
        selected = "4"
        issel = True
    End If
    If selected = "Minecade Mod 1.7.2 with Optifine Standard and Minecade Capes" Then
        selected = "1"
        issel = True
    End If
    If selected = "Minecade Mod 1.7.2 with Optifine Ultra and Minecade Capes" Then
        selected = "2"
        issel = True
    End If

    If issel = False Then
        MsgBox("Invalid Selection! Try again.")

    Else
        Dim answ As Integer
        answ = MsgBox("You have chosen the mod with the ID of: " & selected & "." & vbCrLf & "Do you want to install this mod?", vbYesNo)
        If answ = 6 Then

            If My.Computer.FileSystem.FileExists("C:\Documents and Settings\All Users\Documents\JOWD\MineCadeMod\1.7.2modded" & selected & ".zip") Then
                Dim answOverW As Integer = MsgBox("The file already exists on the download location. Do you wish to download the file again (NO) or do you want to continue with the old one (YES)? (Preferred: Yes)", vbYesNo)
                '6y7n


            End If


            'Installation process begins


            Try
                Dim dlPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "JOWD\MineCadeMod\1.7.2modded" & selected & ".zip"
                My.Computer.Network.DownloadFile("http://files.casualnetwork.net/installers/moddedminec/1.7.2modded" & selected & ".zip", dlPath, "", "", False, 500, True)
                Dim Unpackandinstall As Boolean = MsgBox("Download succesful. Do you want to unpack and install the archieve?", vbYesNo)
                If Unpackandinstall = True Then
                    'UNPACK -------

'''这里的TRY标签内出现错误!'''

                    Try

                        Dim filePath As String
                        filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & ".minecraft\versions\1.7.2modded" & selected
                        Dim startPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "JOWD\MineCadeMod\1.7.2modded" & selected & ".zip"
                        Dim zipPath As String = filePath
                        Dim extractPath As String = filePath

                        My.Computer.FileSystem.CreateDirectory(filePath)

                        ZipFile.CreateFromDirectory(startPath, zipPath)

                        ZipFile.ExtractToDirectory(zipPath, extractPath)
                        MsgBox("Decompression, installation and finishing done! Ready to play!")
                    Catch ex As Exception

                        MsgBox("Error in decompression and installment proceidure." & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Report to JOWD, as this should NOT happen!")
                        Button1.Enabled = True
                        Button2.Enabled = True
                        ComboBox1.Enabled = True
                        Button1.Text = "Download and Install!"

                    End Try

'''错误区结束!'''

                End If
            Catch ex As Exception
                Button1.Enabled = True
                Button2.Enabled = True
                ComboBox1.Enabled = True
                Button1.Text = "Download and Install!"
                MsgBox("Download failed. Error code below!" & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Check the main topic for a possible solution, if nothing applies leave a reply!")
                Exit Sub
            End Try


        Else
            'installation process aborted.


        End If
    End If

End Sub

我很乐意回答与我的问题相关的任何问题,我试图在任何地方寻求帮助,但没有任何帮助!

谢谢。


阅读!已编辑。

关于 David Sdot 和 Visual Vincent 的 2 个答案,-他们的答案并没有解决我的问题。

我尝试在代码中使用以下行:

filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\versions\1.7.2modded" & selected

发生同样的错误。

仍在寻求您的更多建议!

如果您希望项目文件对其进行测试,请留下评论。


阅读!已编辑。

这是应用程序的源代码,在那里进行测试!

http://files.casualnetwork.net/installers/moddedminec/source/MinecadeModInstaller_Min.zip

【问题讨论】:

  • 您是否设置了断点以查看错误出现在哪里?我可以看到您可以从多个地方获得这些信息。
  • 将代码缩小到发生错误的部分。注意MCVE中的M
  • 是的,我想要项目文件。我认为这可能与提取代码有关...
  • 在创建目录上设置断点,然后按 F10 一次,如果没有错误我打赌它会在 ExtractToDirectory 上...让我知道。
  • 我也想要这个文件...

标签: vb.net path appdata


【解决方案1】:

好的,这是我对您问题的解决方案:

在这里,我使用了 WebClient 而不是 My.Computer.Network.DownloadFile,因为我认为它更好。 (当然,你可以使用任何你想要的东西)

Dim Download As New WebClient
Download.DownloadFileAsync(New Uri("http://files.casualnetwork.net/installers/moddedminec/1.7.2modded" & selected & ".zip"), dlPath)

我还注意到您的代码中有一些必须更改的内容。 出于某种原因,您尝试将文件压缩到自身中:

ZipFile.CreateFromDirectory(startPath, zipPath)

删除这个。 :)


您还尝试通过执行以下操作将 .minecraft\versions\1.7.2modded 提取到自身:

    Dim startPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\JOWD\MineCadeMod\1.7.2modded" & selected & ".zip"
    Dim zipPath As String = filePath
    Dim extractPath As String = filePath

    ZipFile.ExtractToDirectory(zipPath, extractPath)

只需从以下位置更改 zipPath:

Dim zipPath As String = filePath

收件人:

Dim zipPath As String = startPath

现在压缩应该可以正常工作了:)


我注意到的另一件事是,即使您在 MsBox 中按“否”,您也无法跳过解压缩部分。所以我稍微改了一下代码:

Dim Unpackandinstall As DialogResult
Unpackandinstall = MessageBox.Show("Download succesful. Do you want to unpack and install the archieve?", "", MessageBoxButtons.YesNo)
If Unpackandinstall = Windows.Forms.DialogResult.Yes Then
    ...
End If

这是整个 Try 块:

        Try
            Dim Download As New WebClient
            Dim dlPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\JOWD\MineCadeMod\1.7.2modded" & selected & ".zip"
            Download.DownloadFileAsync(New Uri("http://files.casualnetwork.net/installers/moddedminec/1.7.2modded" & selected & ".zip"), dlPath)

            Dim Unpackandinstall As DialogResult
            Unpackandinstall = MessageBox.Show("Download succesful. Do you want to unpack and install the archieve?", "", MessageBoxButtons.YesNo)
            If Unpackandinstall = Windows.Forms.DialogResult.Yes Then

                Try

                    Dim filePath As String
                    filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\versions\1.7.2modded" & selected
                    Dim startPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\JOWD\MineCadeMod\1.7.2modded" & selected & ".zip"
                    Dim zipPath As String = startPath
                    Dim extractPath As String = filePath

                    My.Computer.FileSystem.CreateDirectory(filePath)

                    'ZipFile.CreateFromDirectory(startPath, zipPath)

                    ZipFile.ExtractToDirectory(zipPath, extractPath)
                    MsgBox("Decompression, installation and finishing done! Ready to play!")
                Catch ex As Exception

                    MsgBox("Error in decompression and installment proceidure." & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Report to JOWD, as this should NOT happen!")
                    Button1.Enabled = True
                    Button2.Enabled = True
                    ComboBox1.Enabled = True
                    Button1.Text = "Download and Install!"

                End Try

            End If
        Catch ex As Exception
            Button1.Enabled = True
            Button2.Enabled = True
            ComboBox1.Enabled = True
            Button1.Text = "Download and Install!"
            MsgBox("Download failed. Error code below!" & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & "Check the main topic for a possible solution, if nothing applies leave a reply!")
            Exit Sub
        End Try

然后替换

Download.DownloadFileAsync(New Uri("http://files.casualnetwork.net/installers/moddedminec/1.7.2modded" & selected & ".zip"), dlPath)

如果您更愿意使用您的 My.Computer.Network 代码。 :)

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & ".minecraft\versions\1.7.2modded" & selected
    

    Environment.GetFolderPath 返回的路径末尾没有 \,而是在前面添加了一个点。

    filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\minecraft\versions\1.7.2modded" & selected
    

    【讨论】:

    • 1.7.2修改的是文件夹还是文件名的开头一起选中?如果如果文件夹存在另一个反斜杠。另一个问题可能是文件夹名称.minecraft 我知道win explorer 无法创建以点开头的目录,可能与此有关。
    • 文件夹 .minecraft 已经存在,但是 1.7.2modded 不存在。
    猜你喜欢
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多