Search code examples
vb.netpathappdata

VB.NET - Access to path %appdata% is denied


I was making a mod installer for a Minecraft community, when I ended with this problem:

Error description

Here's my code:

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

'''Error occures inside the TRY tags here!'''

                    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

'''Error area ends!'''

                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

I will be happy to answer any question related to my problem, I've tried to look help anywhere but nothing helps me!

Thanks.


Read! Edited.

Regarding the 2 answers from David Sdot and Visual Vincent, - Their answers did not fix my problem.

I tried to use the following line on the code:

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

Same error occurred.

Still looking for more advices from you!

Leave a comment if you want the project file to test it out.


Read! Edited.

Here's the source for the app, do your testing there!

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


Solution

  • Ok so here's my solution to your problem:

    Here, I used a WebClient instead of My.Computer.Network.DownloadFile, since I think it's better. (You use whatever you want of course)

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

    I also noticed some stuff that had to be changed in your code. For some reason you tried to zip your file into itself:

    ZipFile.CreateFromDirectory(startPath, zipPath)
    

    Remove this. :)


    And you also tried to extract .minecraft\versions\1.7.2modded to itself by doing this:

        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)
    

    Simply change zipPath from:

    Dim zipPath As String = filePath
    

    To:

    Dim zipPath As String = startPath
    

    Now the zipping should work fine :)


    One more thing I noticed is that you couldn't skip the unzip part even if you pressed "No" in the MsBox. So I changed that code a little:

    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
    

    Here's the whole Try block:

            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
    

    And just replace

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

    With your My.Computer.Network code if you'd rather use that instead. :)

    Hope this helps!