vb.net downloader
You can create a very cool application using vb.net that is a downloader to download any file/video etc . In this video at idealprogrammer.tv you can see how you can create a downloader program in vb.net with progress bar that shows the downloading process.
If you have any suggestions and comments use idealprogrammer forums
Watch the video here > http://idealprogrammer.tv/videos/483/how-to-make-a-file-downloader-in-vb.net
That video is very basic and you if you are a professional .net developer you may not go with that method because of 2 reasons :
1. That program can freeze your application if download size is big because the process is started from the same thread .
2. You dont have the resume capablity in that way.
So you might go with this method taken from the codeproject

Introduction
I'll explain how you can build a complete, reliable and powerful download manager with .NET Framework.
It is able to show a rarity of data about the download in progress, including the download speed, the file size, the downloaded size, and the percentage of completion.
A completely new class has been written based on this code, which features logic/layout abstraction, multiple file download support, and is more easy to use by providing a variety of properties and events. Check out the FileDownloader article.
The files are downloaded via the HttpWebRequest and HttpWebResponse, and written to a file with the StreamWriter. All this is done on a separate thread (with a BackgroundWorker), so the application using this downloader won't freeze.
Using the Code
The demo application is pretty simple, and contains only the needed UI components for downloading a file. You can of course implement this downloader in your own application, or just use it to download in the background without any user interaction.
The most important code can be found in the DoWork method of the BackgroundWorker. First, an attempt will be made to establish a connection to the file. If this is successful, a script in the Do loop will start downloading the file block by block, until there are no remaining bytes to be downloaded. Note that as soon as a block of data has been received, it's written to the local target file.
| vb | | copy code | | ? |
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _ |
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork |
'Creating the request and getting the response |
Dim theResponse As HttpWebResponse |
Dim theRequest As HttpWebRequest |
Try 'Checks if the file exist |
theRequest = WebRequest.Create(Me.txtFileName.Text) |
theResponse = theRequest.GetResponse |
Catch ex As Exception |
MessageBox.Show("An error occurred while downloading file. _
|
Possible causes:" & ControlChars.CrLf & _ |
"1) File doesn't exist" & ControlChars.CrLf & _ |
"2) Remote server error", "Error", _ |
MessageBoxButtons.OK, MessageBoxIcon.Error) |
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete) |
Me.Invoke(cancelDelegate, True) |
Exit Sub |
End Try |
Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes) |
Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts) |
Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate |
Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create) |
'Replacement for Stream.Position (webResponse stream doesn't support seek) |
Dim nRead As Integer |
'To calculate the download speed |
Dim speedtimer As New Stopwatch |
Dim currentspeed As Double = -1 |
Dim readings As Integer = 0 |
Do |
If BackgroundWorker1.CancellationPending Then 'If user aborts download |
Exit Do |
End If |
speedtimer.Start() |
Dim readBytes(4095) As Byte |
Dim bytesread As Integer = theResponse.GetResponseStream.Read_ |
(readBytes, 0, 4096) |
nRead += bytesread |
Dim percent As Short = (nRead * 100) / length |
Me.Invoke(safedelegate, length, nRead, percent, currentspeed) |
If bytesread = 0 Then Exit Do |
writeStream.Write(readBytes, 0, bytesread) |
speedtimer.Stop() |
readings += 1 |
If readings >= 5 Then 'For increase precision, _ |
' the speed is calculated only every five cycles |
currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000) |
speedtimer.Reset() |
readings = 0 |
End If |
Loop |
'Close the streams |
theResponse.GetResponseStream.Close() |
writeStream.Close() |
If Me.BackgroundWorker1.CancellationPending Then |
IO.File.Delete(Me.whereToSave) |
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete) |
Me.Invoke(cancelDelegate, True) |
Exit Sub |
End If |
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete) |
Me.Invoke(completeDelegate, False) |
End Sub |
This code uses delegates to invoke methods on the main thread, and pass data about the download progress to them.
The code does not support FTP downloads, but can be easily modified to do this.
Resume Downloads
It is possible to modify the code to allow resuming downloads. Add this code before the first use of the HttpWebRequest object.
| vb | | copy code | | ? |
theRequest.AddRange(whereYouWantToStart) '<- add this |
You'll also need to set the
Position property of the FileStream instance to the position where you want to resume the download. So be sure you also save this before the download is cancelled.| vb | | copy code | | ? |
Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Open) |
writeStream.Position = whereYouWantToStart |
Calculating Speed
The code contains built in download speed calculation, using the StopWatch class. This will return another result every time 5 packages have been downloaded. Note that you can also manually calculate the speed, and use a timer to get data at a more regular interval.
Well if you have any questions or suggestions please use the comments or idealprogrammer forums
Related posts:
- Build a bluetooth Chat Application Bluetooth is one of today's most exciting technologies. It...
- Visual Studio Express 2005 Video Tutorial – Part 4 – Adding Text-To-Speech (TTS) In this tutorial, we learn how to add speech to...
Related posts brought to you by Yet Another Related Posts Plugin.
