# [.net] .NET: Retrieving a file from internet...

## Recommended Posts

For a project I am working on I would like to retrieve an initial configuration file from a particular URL on the application's first execution. Can anyone get me pointed down the correct path for this? The .NET framework has quite a robust web related namespace and I'm not really sure where to start, but I imagine it would be quite simple. Thanks for any help. Oh, also... I was thinking about implementing some sort of automatic update type system. Does anyone know of any good source of info on that sort of thing?

##### Share on other sites
Yeah, it's really easy it seems to get files from the web. Try using the FileWebRequest class, it handles getting access to files from the web. I'm not sure about the update system, but there are plenty of ways to do it. Could just have a file online that lists changed files for each version update, download the file and then download all of the required files to get up to date.

##### Share on other sites
For the downloading itself I like the code listed below. For an update system, what I like to do is put an update file on the webserver that contains a list of files with their relative path. Each item has the file, its size, and an MD5 checksum. When the applicatin needs to update, it pulls down the hash file (example) it checks each file listed. If the local file's checksum or size doesn't match, then the application adds it to a list of files to download. Because the keyfile contains sizes, the app knows the total number of bytes it needs to download, so it can use the code below, and do some of its own calculations to show a real nice set progress bars when it starts downloading files.

Private Function DownloadFileWithProgress(ByVal URL As String, ByVal Location As String, ByVal Progress As ProgressBar) As Boolean        'this function based on code from        'HSAA - Colin Harman MACITP - Rollershade         Try            Dim wRemote As System.Net.WebRequest            Dim bBuffer(999) As Byte            Dim iBytesRead As Integer            wRemote = System.Net.WebRequest.Create(URL)            Dim sw As System.IO.Stream = New System.IO.FileStream(Location, IO.FileMode.Create, IO.FileAccess.Write)            Dim myWebResponse As System.Net.WebResponse = wRemote.GetResponse()            Progress.Minimum = 0            Progress.Value = 0            Progress.Maximum = CInt(myWebResponse.ContentLength)            Dim sChunks As System.IO.Stream = myWebResponse.GetResponseStream()            Do                iBytesRead = sChunks.Read(bBuffer, 0, 256)                If Progress.Value + iBytesRead <= Progress.Maximum Then                    Progress.Value += iBytesRead                Else                    Progress.Value = Progress.Maximum                End If                Progress.Refresh()                sw.Write(bBuffer, 0, iBytesRead)            Loop Until iBytesRead = 0            Progress.Value = Progress.Maximum            sChunks.Close()            sw.Flush()            sw.Close()            Return True        Catch ex As Exception            Return False        End Try    End Function

##### Share on other sites
intrest86:

It looks like the FileWebRequest class is used to handle local files as a web request. Am I missing something? It just doesn't look like it actually handles files on the web. I'll give it a good looking over, though.

zangetsu:

Thanks for the code snippet! Unfortunately, it will be a little while before I can give it a go, but it looks simple enough to me.

Also, thanks for the update ideas. I guess it's really just a simple matter of inventing my own system and retrieving the files.

##### Share on other sites
Quote:
 Original post by smitty1276intrest86:It looks like the FileWebRequest class is used to handle local files as a web request. Am I missing something? It just doesn't look like it actually handles files on the web. I'll give it a good looking over, though.

You're right, sorry for the mistake. Turns out there is another interface more to handle web files, WebClient.DownloadFile. This one is definately meant for downloading files from FTP and HTTP, so it should do the job. Note that this function is blocking, but it has an Async counterpart if you can't have that.

Hope this helps.

##### Share on other sites
Ever heard of smart client?

Or ClickOnce?

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627654
• Total Posts
2978443

• 10
• 12
• 22
• 13
• 33