BP Forums
Using the WebClient Class To Track Download Progress - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=18)
+----- Thread: Using the WebClient Class To Track Download Progress (/showthread.php?tid=808)



Using the WebClient Class To Track Download Progress - brandonio21 - 08-23-2013

Using this code snippet, you should be able to download any file to your computer and track it's progress using a progressbar or a label (Or any other control which you desire). Below is a sample snippet, so be sure to change variable names to match your project!

[code2=vbnet]Imports System.Net
Public Class Form1

Private Sub btn_downloadStart_Click(sender As System.Object, e As System.EventArgs) Handles btn_downloadStart.Click
Dim wc As New WebClient 'This allows us to actually download the files
AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgressChanged 'Link the progresschanged event with our subroutine
AddHandler wc.DownloadFileCompleted, AddressOf DownloadFileCompleted 'Link the downloadfilecompleted event with our subrouting
wc.DownloadFileAsync(New System.Uri("File URL"), "Local Path") 'Actually download the file (on another thread), change File URL and Local Path to your information
End Sub

Public Sub DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
progress_download.Value = e.ProgressPercentage 'Displays download progress on a progress bar
lbl_details.Text = e.BytesReceived & "/" & e.TotalBytesToReceive 'Displays download progress (as fraction) in a label
End Sub

Public Sub DownloadFileCompleted(sender As Object, e As EventArgs)
MsgBox("The file has completed downloading!") 'Tell the user the download is complete
End Sub
End Class[/code2]

A video tutorial for this code snippet should be coming soon!