Using the following sub to download a file I receive an "Access to the path 'path' is denied" no matter where I try to download it to even if I run the program as an administrator.

Why do I get an access denied error?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("HLDSUpdateTool is Copyright © VALVe Corporation. We are not affiliated with VALVe Corporation in any way.", MsgBoxStyle.Exclamation, "Copyright")
        MsgBox("Select where you would like to download this file to.", MsgBoxStyle.Information, "Download")
        Dim FolderBrowserDialog1 As New FolderBrowserDialog
        With FolderBrowserDialog1
            .RootFolder = Environment.SpecialFolder.Desktop
            If .ShowDialog = DialogResult.OK Then
            End If

            DownloadFile("http://storefront.steampowered.com/download/hldsupdatetool.exe", .SelectedPath)
            hldsupdatetool = .SelectedPath & "\hldsupdatetool.exe"
            My.Settings.HLDSUpdate = .SelectedPath & "\hldsupdatetool.exe"
            If My.Computer.FileSystem.FileExists(.SelectedPath & "\hldsupdatetool.exe") = True Then
                MsgBox("HLDSUpdateTool successfuly downloaded to """ & .SelectedPath & """.", MsgBoxStyle.Information, "Download")
                MsgBox("An error occurred downloading HLDSUpdateTool.", MsgBoxStyle.Exclamation, "Download")
            End If
        End With
    End Sub
6 Years
Discussion Span
Last Post by Darkicon

Most likely problem lies within the DownloadFile method.
Perhaps it's the WebClient that doesn't have access to the file your'e trying to download.
Try adding some credentials to the client.

Dim cli As System.Net.WebClient
            cli.Credentials = New System.Net.NetworkCredential("username", "password")
            cli.DownloadFile("url to file", "local file")

Whoops, I probably should've included the actual sub rather than the button's sub.

Public Sub DownloadFile(ByVal URL As String, ByVal Location As String)
            Dim wr As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)
            Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
            Dim str As Stream = ws.GetResponseStream()
            Dim inBuf(100000) As Byte
            Dim bytesToRead As Integer = CInt(inBuf.Length)
            Dim bytesRead As Integer = 0
            While bytesToRead > 0
                Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
                If n = 0 Then
                    Exit While
                End If
                bytesRead += n
                bytesToRead -= n
            End While
            Dim fstr As New FileStream(Location, FileMode.OpenOrCreate, FileAccess.Write)
            fstr.Write(inBuf, 0, bytesRead)
        Catch ex As Exception
        End Try
    End Sub

Wow, nevermind, I didn't realize I could've done this much simpler by using My.Computer.Network.DownloadFile.

Edited by Darkicon: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.