Code is downloading every file at the same time

  • Thread starter Thread starter Broggy69
  • Start date Start date
B

Broggy69

Guest
Code is downloading every file at the same time.

I am not sure what I am doing wrong. I just want to download a file then after it is done continue to next file.

You'll see in the code I have an array of files I need to download. These never change (Or very rarely change). I only want one file to download at a time.

Here is the code:

Imports System.Net
Imports System.ComponentModel


Public Class Form1

Dim WithEvents WClient As New WebClient


Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim ftpsite As String = "ftp://myftp.com/"

'*** For Third Party Files ***
Dim DB_3rd_FTPFiles() As String = {"File1",
"File2",
"File3",
"FIle4"}

'These are different because they are in a different folder
Dim DB_BB_FTPFiles() As String = {"File5",
"File6",
"File7"}

With My.Computer.FileSystem
.CreateDirectory("C:\FakeEDrive\Install")
.CreateDirectory("C:\FakeEDrive\Install\Folder1\")
.CreateDirectory("C:\FakeEDrive\Install\Folder2\")
.CreateDirectory("C:\FakeEDrive\Install\Folder3\")
.CreateDirectory("C:\FakeEDrive\Install\Folder4\")

End With

For Each ThirdFTP As String In DB_3rd_FTPFiles
If ThirdFTP = "File2.zip" Then
lbl_progress.Text = "Downloading file: " & ThirdFTP
WClient = New WebClient


Dim Downloadpath2 As New Uri(ftpsite & "SubFolder/" & ThirdFTP)
AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
WClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)")
WClient.Credentials = New NetworkCredential("user", "password")
WClient.DownloadFileAsync(Downloadpath2, "C:\FakeEDrive\Install\Folder1\" & ThirdFTP)

WClient.Dispose()
End If

WClient = New WebClient
WClient.Credentials = New NetworkCredential("user", "password")
Dim Downloadpath As New Uri(ftpsite & ThirdFTP)
AddHandler WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete

Try
WClient.DownloadFileAsync(Downloadpath, "C:\FakeEDrive\Install\Folder2\" & ThirdFTP)

Catch ex As Exception
MsgBox(ex.ToString)
End Try

WClient.Dispose()
Next

For Each BBTSFTP As String In DB_BB_FTPFiles
lbl_progress.Text = "Downloading file: " & BBTSFTP
WClient = New WebClient
WClient.Credentials = New NetworkCredential("user", "password")
Dim Downloadpath As New Uri(ftpsite & "Folder/" & BBTSFTP)
AddHandler _WClient.DownloadProgressChanged, AddressOf WClient_ProgressChanged
AddHandler _WClient.DownloadFileCompleted, AddressOf WC_DownloadComplete

Try
WClient.DownloadFileAsync(Downloadpath, "C:\FakeEDrive\Install\folder3\" & BBTSFTP)
Catch ex As Exception
MsgBox(ex.ToString)
End Try

WClient.Dispose()
Next

End Sub

Public Sub WC_DownloadComplete(sender As Object, e As AsyncCompletedEventArgs)

Using webClient As New Net.WebClient
RemoveHandler webClient.DownloadFileCompleted, AddressOf WC_DownloadComplete
End Using

End Sub

Private Sub WClient_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)

ProgressBar1.Value = e.ProgressPercentage

End Sub

End Class

Continue reading...
 
Back
Top