locked
Getting an Error When Downloading a File RRS feed

  • Question

  • Hello, I am making a launcher for a program I will be making in the near future.  It's supposed to 'check for updates' by downloading a file and checking if the version number in it matches the program's version.  If it does not match (update found), then it downloads the updated files and replaces them with the current program files.  I am stuck at the first part:  downloading the version file.  This is the line of code: 
    My.Computer.Network.DownloadFile("<url>", "C:\version.txt")
    There isn't a username and password.
    This is the error I receive when this line executes: 
    An unhandled exception of type 'System.Net.WebException' occurred in System.dll
    
    Additional information: An exception occurred during a WebClient request.

    ~ TechPerson32

    Tuesday, November 5, 2013 2:48 AM

Answers

  • TP32,

    You don't need to "download" it (in the traditional sense), just read it directly:

    Private Function ReturnStringFromURL(ByVal url As String) As String Dim retVal As String Dim sb As New System.Text.StringBuilder Try Dim request As WebRequest = WebRequest.Create(url) Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse) Using dataStream As Stream = response.GetResponseStream Using rdr As New StreamReader(dataStream) sb.Append(rdr.ReadToEnd) End Using End Using End Using retVal = sb.ToString Catch ex As Exception retVal = Nothing End Try Return retVal End Function



    Please call me Frank :)

    • Marked as answer by Greg Whatley Tuesday, November 5, 2013 3:20 AM
    Tuesday, November 5, 2013 2:55 AM

All replies

  • TP32,

    You don't need to "download" it (in the traditional sense), just read it directly:

    Private Function ReturnStringFromURL(ByVal url As String) As String Dim retVal As String Dim sb As New System.Text.StringBuilder Try Dim request As WebRequest = WebRequest.Create(url) Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse) Using dataStream As Stream = response.GetResponseStream Using rdr As New StreamReader(dataStream) sb.Append(rdr.ReadToEnd) End Using End Using End Using retVal = sb.ToString Catch ex As Exception retVal = Nothing End Try Return retVal End Function



    Please call me Frank :)

    • Marked as answer by Greg Whatley Tuesday, November 5, 2013 3:20 AM
    Tuesday, November 5, 2013 2:55 AM
  • Thank you!  :)

    ~ TechPerson32

    Tuesday, November 5, 2013 3:20 AM
  • Hello Frank:

    Maybe the result isn't a string, perhaps it's made up of binary things, so you cannot read directly:)

    Hello TechPerson32:

    Can you give us a detail Url for us to have a test with?


    For Account Validation, please follow "Verify Account+Number" at http://social.msdn.microsoft.com/Forums/en-us/home?forum=reportabug

    For ASP.NET Question, please ask at http://forums.asp.net

    For other questions, you can find a specific forum and then ask at http://stackexchange.com/sites

    Click and Donate at http://www.freerice.com

    Tuesday, November 5, 2013 3:22 AM
  • Thank you!  :)

    ~ TechPerson32



    Please call me Frank :)

    Tuesday, November 5, 2013 3:23 AM
  • Hello Frank:

    Maybe the result isn't a string, perhaps it's made up of binary things, so you cannot read directly:)

    Well he's saving it as a string:

    My.Computer.Network.DownloadFile("<url>", "C:\version.txt")


    Please call me Frank :)

    Tuesday, November 5, 2013 3:25 AM
  • My.Computer.Network.DownloadFile("<url>", "C:\version.txt")

    If version.txt already exists you need to set OVERWRITE to true.

    http://msdn.microsoft.com/en-US/library/ms127881(v=vs.80).aspx

    • Proposed as answer by ThankfulHeart Tuesday, November 5, 2013 3:34 AM
    • Edited by tommytwotrain Tuesday, November 5, 2013 3:34 AM changed link
    Tuesday, November 5, 2013 3:29 AM