Sign in to follow this  

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

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this