Question WebClient Exception net4.0

holyraider

New member
Joined
Jan 18, 2012
Messages
3
Programming Experience
Beginner
Hi there,
I work with Visual studio 2010 and net4.0 with Visual Basic as language.
im new to dotnet and at the moment im coding a application which does some calculations.
I try to implement a "update" feature which downloads the most recent .xml file where i get the numbers i need for the calculations from.
everything works absolutly fine, only my Update/download code seems to have some issue i cant fix.
I only include the small part of the code which is relevant to the webclient.
Here are my imports and the Module:


VB.NET:
Module Module1
    Sub Main()
        Dim FileAdress As String = String.Empty
        Dim FileName As String = String.Empty
        Try
            Dim FileReader As New WebClient()
            FileAdress = "[URL]http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml[/URL]"
            FileName = FileAdress.Substring(FileAdress.LastIndexOf("/") + 1)
            FileReader.DownloadFile(FileAdress, "C:\" + FileName)
        Catch ex As HttpListenerException
            Console.WriteLine("Error Accessing " + FileAdress + "-" + ex.Message)
        Catch Ex As Exception
            Console.WriteLine("Error Accessing " + FileAdress + "-" + Ex.Message)
        End Try
    End Sub
End Module


here is the event which executes the download:
VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles bnUpdate.Click
        Dim FileAdress As String = String.Empty
        Dim FileName As String = String.Empty
        Try
            Dim FileReader As New WebClient()
            FileAdress = "[URL]http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml[/URL]"
            FileName = FileAdress.Substring(FileAdress.LastIndexOf("/") + 1)
            FileReader.DownloadFile(FileAdress, "C:\")
        Catch ex As HttpListenerException
            MessageBox.Show("Download Error")
            Console.WriteLine("Error Accessing " + FileAdress + "-" + ex.Message)
---->>>>    Catch Ex2 As Exception
            MessageBox.Show(Ex2.StackTrace)
            Console.WriteLine("Error Accessing " + FileAdress + "-" + Ex2.Message)
        End Try
    End Sub
---->>>>> At that point it throws an exception:
WebException was caught
"An exception occurred during a WebClient request."
Stacktrace :
"at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at System.Net.WebClient.DownloadFile(String address, String fileName)
at Devisenrechner02.Form1.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\*****\...

I cant find the problem and the exception info isnt very informative i think.
Hope you can help me and i also hope i posted in the right section..im just very new to programming and have no clue :)
Thanks in Advance
holyraider
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
What is the second parameter of the WebClient.DownloadFile method?
fileName
Type: System.String
The name of the local file that is to receive the data.
Now let's look at your code:
VB.NET:
FileReader.DownloadFile(FileAdress, [B][U]"C:\"[/U][/B])
Is that the name of a file?
 

holyraider

New member
Joined
Jan 18, 2012
Messages
3
Programming Experience
Beginner
i already tried doing it with a filename. at first i used ("C:\" + Filename) but that didnt worked out. Atm its ("C:\devisen.xml"). By the time i copied the code and posted here i was trying it out without a filename.
Both methods didn't work.

:/

edit: i just noticed that i can see more info when debugging. Access is denied.
So i guess i just need to give it a filepath where it has fullrights.
gonna try out.

mhh.. how can i ensure that the program always has the rights to download the file to the specified path?
Hope you know what i mean:S
 
Last edited:

holyraider

New member
Joined
Jan 18, 2012
Messages
3
Programming Experience
Beginner
you can close the thread. It didnt worked because i didnt have write rights on C:\ . After i granted them everything worked just fine.
Thanks anyway, i might had stopped looking into it.
 
Top Bottom