The .NET Framework has always made it easy to download files. But Dot.NET 2.0 added a new Async version of the DownloadFile feature. That basically means that if you use the DownloadFileAsync() sub it will download on a separate thread instead of consuming your application thread which makes your application appear frozen. If you used the regular DownloadFile and didn’t want your app’s thread tied up while downloading, you would have to spawn a new thread for the download. So Microsoft made it a little easier to download a file by adding the DownloadFileAsync method. This article and example works with Visual Basic 2008, VB 2010, and Visual Basic 2013 as well.
Updated 2015: I recently added some extra code to this article near the end. The code will add handlers for the DownloadProgress and DownloadCompleted events.
This is a simple article that will briefly highlight Downloading Files but the bulk of the code is in a VB.NET example program I made. There is way to much code to add here. The example application will not only show how to download files using the DownloadFileAsync method, but also how to add advanced features and capabilities that are not explicitly available in the DotNet 2.0/3.5/4.0 framework.
Below are some of the major features of the example project…
- Download Progress updates like how many Bytes has been downloaded and the total size of the download.
- Calculate the approximate download speed.
- Calculate how long it will take the download to complete.
- Keep track of how long the download has been active.
- Keeping track of the Peak download speed.
- Keeping track of the Average download speed.
- Getting the current Downloading Status
- Keeping track of the downloading progress and when its finished and more!
This is the base DownloadFileAsync sub I used below…
Public Sub DownloadFileAsync(ByVal address As System.Uri, ByVal fileName As String)
That method is part of the: System.Net.Webclient class. Webclient contains some very easy to use methods for uploading, downloading, and etc.
While the ‘MY’ feature has a default instance of DownloadFile under: My.Computer.Network, it unfortunately doesn’t have the DownloadFileAsync instance. So first of all to get started downloading you need to create a new instance of the Webclient class.
' 'Create a new instance of the Webclient class. Dim downloading As New Net.WebClient
You now have the DownloadFileAsync feature at your fingertips. Just simply call the subroutine while specifying the required parameters. Check the example code below…
' 'Will be used to setup the download url. Dim uriSource As Uri ' 'Setup a new Uniform Resource Identifier with the address to the file you want to download. 'By the way the link in the code snippet below doesn’t work. uriSource = New Uri("http://www.vbcodesource.com/myFileToDownload.exe")
You can now get started performing the actual downloading.
' 'By using the Async version of DownloadFile your applications thread won’t be focused 'on the download and thus you can still interact with your application. The only way 'to get that feature with the regular DownloadFile method is by running it in a 'seperate thread which is no longer required since an async version is availble. downloading.DownloadFileAsync(uriSource, "c:\MyDownloadedFile.exe")
That’s really all there is to it to get basic downloading support for your applications. If you want download progress, size, speed, time left till the download is finished and more, click this link to download the example project I mentioned earlier made with Visual Basic 2005. The program can be used in Visual Basic 2008 and Visual Basic 2010 as well.
I wanted to mention the Webclient class has two events you can use. They are Download Progress and Download Completed.
Public Event DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
Public Event DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
All you have to do is Add Handler’s to each of those events to get reports each time data is received and when the file has finished downloading.
Edit: I added some simple code below on how to setup the two handlers.
Here are the Subroutines fired when the events are activated.
'This will be fired whenever the DownloadProgress based event is hit. Sub getDownloadProgress(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) 'Update the progress bar and percentage label. pbDownloadProgress.Value = e.ProgressPercentage End Sub
'Will fire when the download has ended. Sub downloadHasEnded(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) 'Display the download has finished or was canceled. If e.Cancelled Then ' 'Show that the download had been canceled. Msgbox ("Status: Canceled") Else 'Show that the download finished gracefully. Msgbox ("Status: Finished!") End If End Sub
This will set the above Subs to handle the events.
'This sub will setup the event handlers for the program. Sub setupEventHandlers() 'The getDownloadProgress Sub will fire whenever the DownloadAsync method updates the file 'download status. AddHandler downloading.DownloadProgressChanged, AddressOf getDownloadProgress 'The downloadHasEnded Sub is fired each time a download finishes. AddHandler downloading.DownloadFileCompleted, AddressOf downloadHasEnded End Sub
The application example highlights much more than the code in this article. So if you want, or ever needed to download file from the internet then this Link will get you a example project with full source code. Have Fun!