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

Started by
4 comments, last by paulecoyote 17 years, 10 months ago
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?
Advertisement
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.
Turring Machines are better than C++ any day ^_~
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
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.
Quote:Original post by smitty1276
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.

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.
Turring Machines are better than C++ any day ^_~
Ever heard of smart client?

Or ClickOnce?
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.

This topic is closed to new replies.

Advertisement