VB.NET - 拒绝访问路径%appdata%

时间:2014-06-29 15:11:50

标签: vb.net path permissions appdata denied

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

Error description

这是我的代码:

    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

2 个答案:

答案 0 :(得分:0)

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 :(得分:0)

好的,这是我解决问题的方法:

在这里,我使用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.2 modded:

    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

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


我注意到的另一件事是即使你按下了#34; No"你也不能跳过解压缩部分。在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代码,如果您更愿意使用它。 :)

希望这有帮助!