How To Download A File (2 Different Ways) - VB#22 ~ VBTheory™ Tutorials

Friday, September 26, 2014

How To Download A File (2 Different Ways) - VB#22


Code:

Synchronous:
Dim w As New WebClient
w.DownloadFile("https://dl.dropboxusercontent.com/u/58717780/tutorial.txt", "C:\Windows\Temp\test.txt")
Process.Start("C:\Windows\Temp\test.txt")

Asynchronous:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim w As New WebClient

  AddHandler w.DownloadFileCompleted, AddressOf DownloadComplete
  AddHandler w.DownloadProgressChanged, AddressOf ProgressChanged

  w.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/u/58717780/tutorial.txt"), "C:\Windows\Temp\test.txt")
End Sub

Private Sub DownloadComplete(sender As Object, e As   System.ComponentModel.AsyncCompletedEventArgs)
  Process.Start("C:\Windows\Temp\test.txt")
End Sub

Private Sub ProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
  ProgressBar1.Value = e.ProgressPercentage
End Sub



Share