Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'frmBGWorkerDownload Code
- '-------------------------------------------------------------------
- Imports System.ComponentModel
- Imports System.Globalization
- Imports System.IO
- Imports System.Net
- Imports System.Net.Security
- Imports System.Security.Cryptography.X509Certificates
- Public Class frmBGWorkerDownload
- Friend WithEvents BWorkerD As BackgroundWorker
- Public Sub New()
- InitializeComponent()
- BWorkerD = New BackgroundWorker With {
- .WorkerReportsProgress = True,
- .WorkerSupportsCancellation = True
- }
- End Sub
- Private Class BGWorkerObject
- Public Property UserName As String
- Public Property Password As String
- Public Property ResourceURI As String
- Public Property FilePath As String
- Public Property FileLength As Long
- Public Property DownloadedBytes As Long
- Public Property BytesToDownload As Long
- End Class
- 'ftp://185.201.11.24
- 'User: u171165696
- 'Pass: KU7KXW38
- 'File: Blender2.blend
- Private Sub btnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click
- pBarD.Value = 0
- Dim BGWorkerObj As BGWorkerObject = New BGWorkerObject With {
- .ResourceURI = txtFilePathD.Text,
- .FilePath = Path.Combine(txtSavePathD.Text, Path.GetFileName(txtFilePathD.Text)),
- .UserName = txtFTPUsernameD.Text,
- .Password = txtFTPPasswordD.Text
- }
- AddHandler BWorkerD.DoWork, AddressOf BWorkerD_DoWork
- AddHandler BWorkerD.ProgressChanged, AddressOf BWorkerD_ProgressChanged
- AddHandler BWorkerD.RunWorkerCompleted, AddressOf BWorkerD_RunWorkerCompleted
- BWorkerD.RunWorkerAsync(BGWorkerObj)
- End Sub
- Private Sub BWorkerD_DoWork(sender As Object, e As DoWorkEventArgs)
- Dim BGW As BackgroundWorker = TryCast(sender, BackgroundWorker)
- Dim BGWorkerObj As BGWorkerObject = TryCast(e.Argument, BGWorkerObject)
- Dim FTPRequest As FtpWebRequest
- Dim BufferSize As Integer = 131072
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
- ServicePointManager.ServerCertificateValidationCallback =
- Function(snd As Object, Cert As X509Certificate, Chain As X509Chain, Err As SslPolicyErrors)
- Return True
- End Function
- FTPRequest = DirectCast(WebRequest.Create(BGWorkerObj.ResourceURI), FtpWebRequest)
- FTPRequest.Credentials = New NetworkCredential(BGWorkerObj.UserName, BGWorkerObj.Password)
- FTPRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
- FTPRequest.EnableSsl = True
- 'Using FtpResponse As WebResponse = FTPRequest.GetResponse
- ' BGWorkerObj.FileLength = Convert.ToInt64(FtpResponse.ContentLength)
- ' BGW.ReportProgress(0, BGWorkerObj)
- 'End Using
- Using FtpResponse As WebResponse = FTPRequest.GetResponse
- Using DirListStream As Stream = FtpResponse.GetResponseStream()
- Using listReader As StreamReader = New StreamReader(DirListStream)
- While Not listReader.EndOfStream
- Dim DirContent As String = listReader.ReadLine()
- If DirContent.Contains(Path.GetFileNameWithoutExtension(BGWorkerObj.ResourceURI)) Then
- BGWorkerObj.FileLength = Convert.ToInt64(DirContent.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)(4))
- BGW.ReportProgress(0, BGWorkerObj)
- Exit While
- End If
- End While
- End Using
- End Using
- End Using
- If BGW.CancellationPending Then e.Cancel = True
- Try
- FTPRequest = CType(WebRequest.Create(BGWorkerObj.ResourceURI), FtpWebRequest)
- FTPRequest.EnableSsl = True
- FTPRequest.Credentials = New NetworkCredential(BGWorkerObj.UserName, BGWorkerObj.Password)
- FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile
- Using Response As FtpWebResponse = DirectCast(FTPRequest.GetResponse, FtpWebResponse)
- If Response.StatusCode > 299 Then
- e.Result = 0
- Throw New Exception("The Ftp Server rejected the request. StatusCode: " &
- Response.StatusCode.ToString(),
- New InvalidOperationException(Response.StatusCode.ToString()))
- Exit Sub
- End If
- Using stream = Response.GetResponseStream
- Using fileStream As FileStream = File.Create(BGWorkerObj.FilePath)
- Dim read As Integer
- Dim buffer As Byte() = New Byte(BufferSize - 1) {}
- Do
- read = stream.Read(buffer, 0, buffer.Length)
- fileStream.Write(buffer, 0, read)
- BGWorkerObj.DownloadedBytes += read
- BGWorkerObj.BytesToDownload = BGWorkerObj.FileLength - BGWorkerObj.DownloadedBytes
- If BGW.CancellationPending Then
- e.Cancel = True
- Exit Do
- Else
- If BGWorkerObj.FileLength > 0 Then
- BGW.ReportProgress(CInt((CSng(BGWorkerObj.DownloadedBytes) / BGWorkerObj.FileLength) * 100), BGWorkerObj)
- Else
- BGW.ReportProgress(0, BGWorkerObj)
- End If
- End If
- Loop While read > 0
- End Using
- End Using
- End Using
- Catch ex As Exception
- If e.Cancel = False Then Throw
- Finally
- If e.Cancel = True Then
- If File.Exists(BGWorkerObj.FilePath) Then
- File.Delete(BGWorkerObj.FilePath)
- End If
- End If
- End Try
- End Sub
- Private Sub BWorkerD_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
- pBarD.Value = e.ProgressPercentage
- lblPercentD.Text = e.ProgressPercentage.ToString() & " %"
- If lblFileSizeD.Text.Length = 0 Then
- lblFileSizeD.Text = CType(e.UserState, BGWorkerObject).FileLength.ToString("N0", CultureInfo.CurrentUICulture.NumberFormat)
- End If
- lblDownloadedBytesD.Text = CType(e.UserState, BGWorkerObject).DownloadedBytes.ToString("N0", CultureInfo.CurrentUICulture.NumberFormat)
- If e.ProgressPercentage <= 15 Then
- lblDownloadedBytesD.ForeColor = Color.Red
- ElseIf e.ProgressPercentage <= 66 Then
- lblDownloadedBytesD.ForeColor = Color.Orange
- Else
- lblDownloadedBytesD.ForeColor = Color.LightGreen
- End If
- End Sub
- Private Sub BWorkerD_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
- Dim DownloadAborted As Boolean = False
- If e.Error IsNot Nothing Then
- DownloadAborted = True
- lblDownloadedBytesD.ForeColor = Color.Red
- lblDownloadedBytesD.Text = "Error!"
- ElseIf e.Cancelled Then
- DownloadAborted = True
- lblDownloadedBytesD.ForeColor = Color.Yellow
- lblDownloadedBytesD.Text = "Cancelled!"
- pBarD.Value = 0
- lblPercentD.Text = "0%"
- Else
- lblDownloadedBytesD.ForeColor = Color.LightGreen
- lblDownloadedBytesD.Text = "Download completed"
- End If
- RemoveHandler BWorkerD.DoWork, AddressOf BWorkerD_DoWork
- RemoveHandler BWorkerD.ProgressChanged, AddressOf BWorkerD_ProgressChanged
- RemoveHandler BWorkerD.RunWorkerCompleted, AddressOf BWorkerD_RunWorkerCompleted
- End Sub
- Private Sub btnAbortDownload_Click(sender As Object, e As EventArgs) Handles btnAbortDownload.Click
- BWorkerD.CancelAsync()
- End Sub
- Private Sub btnOpenPath_Click(sender As Object, e As EventArgs) Handles btnOpenPath.Click
- Using fbDialog As FolderBrowserDialog = New FolderBrowserDialog()
- fbDialog.ShowNewFolderButton = True
- If fbDialog.ShowDialog(Me) <> DialogResult.OK Then Return
- txtSavePathD.Text = fbDialog.SelectedPath
- End Using
- End Sub
- End Class
- '-------------------------------------------------------------------
- 'frmBGWorkerDownload Designer Code
- '-------------------------------------------------------------------
- <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
- Partial Class frmBGWorkerDownload
- Inherits System.Windows.Forms.Form
- <System.Diagnostics.DebuggerNonUserCode()>
- Protected Overrides Sub Dispose(ByVal disposing As Boolean)
- Try
- If disposing AndAlso components IsNot Nothing Then
- components.Dispose()
- End If
- Finally
- MyBase.Dispose(disposing)
- End Try
- End Sub
- Private components As System.ComponentModel.IContainer
- <System.Diagnostics.DebuggerStepThrough()>
- Private Sub InitializeComponent()
- Me.btnDownload = New System.Windows.Forms.Button()
- Me.txtFTPUsernameD = New System.Windows.Forms.TextBox()
- Me.txtFTPPasswordD = New System.Windows.Forms.TextBox()
- Me.lblFileSizeD = New System.Windows.Forms.Label()
- Me.Label1 = New System.Windows.Forms.Label()
- Me.pBarD = New System.Windows.Forms.ProgressBar()
- Me.Label2 = New System.Windows.Forms.Label()
- Me.lblDownloadedBytesD = New System.Windows.Forms.Label()
- Me.txtFilePathD = New System.Windows.Forms.TextBox()
- Me.Label4 = New System.Windows.Forms.Label()
- Me.Label5 = New System.Windows.Forms.Label()
- Me.Label6 = New System.Windows.Forms.Label()
- Me.Label3 = New System.Windows.Forms.Label()
- Me.txtSavePathD = New System.Windows.Forms.TextBox()
- Me.lblPercentD = New System.Windows.Forms.Label()
- Me.btnAbortDownload = New System.Windows.Forms.Button()
- Me.btnOpenPath = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'btnDownload
- '
- Me.btnDownload.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
- Me.btnDownload.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
- Me.btnDownload.FlatStyle = System.Windows.Forms.FlatStyle.Flat
- Me.btnDownload.Location = New System.Drawing.Point(25, 305)
- Me.btnDownload.Name = "btnDownload"
- Me.btnDownload.Size = New System.Drawing.Size(111, 38)
- Me.btnDownload.TabIndex = 0
- Me.btnDownload.Text = "Download"
- Me.btnDownload.UseVisualStyleBackColor = True
- '
- 'txtFTPUsernameD
- '
- Me.txtFTPUsernameD.BackColor = System.Drawing.Color.DarkOliveGreen
- Me.txtFTPUsernameD.ForeColor = System.Drawing.Color.White
- Me.txtFTPUsernameD.Location = New System.Drawing.Point(24, 38)
- Me.txtFTPUsernameD.Name = "txtFTPUsernameD"
- Me.txtFTPUsernameD.Size = New System.Drawing.Size(231, 23)
- Me.txtFTPUsernameD.TabIndex = 1
- Me.txtFTPUsernameD.Text = "u171165696"
- '
- 'txtFTPPasswordD
- '
- Me.txtFTPPasswordD.BackColor = System.Drawing.Color.DarkOliveGreen
- Me.txtFTPPasswordD.ForeColor = System.Drawing.Color.White
- Me.txtFTPPasswordD.Location = New System.Drawing.Point(277, 38)
- Me.txtFTPPasswordD.Name = "txtFTPPasswordD"
- Me.txtFTPPasswordD.Size = New System.Drawing.Size(231, 23)
- Me.txtFTPPasswordD.TabIndex = 2
- Me.txtFTPPasswordD.Text = "KU7KXW38"
- '
- 'lblFileSizeD
- '
- Me.lblFileSizeD.BackColor = System.Drawing.Color.FromArgb(CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer))
- Me.lblFileSizeD.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
- Me.lblFileSizeD.ForeColor = System.Drawing.Color.White
- Me.lblFileSizeD.Location = New System.Drawing.Point(78, 213)
- Me.lblFileSizeD.Name = "lblFileSizeD"
- Me.lblFileSizeD.Size = New System.Drawing.Size(164, 24)
- Me.lblFileSizeD.TabIndex = 3
- Me.lblFileSizeD.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
- '
- 'Label1
- '
- Me.Label1.AutoSize = True
- Me.Label1.ForeColor = System.Drawing.Color.White
- Me.Label1.Location = New System.Drawing.Point(21, 218)
- Me.Label1.Name = "Label1"
- Me.Label1.Size = New System.Drawing.Size(51, 15)
- Me.Label1.TabIndex = 4
- Me.Label1.Text = "File Size:"
- '
- 'pBarD
- '
- Me.pBarD.ForeColor = System.Drawing.Color.Red
- Me.pBarD.Location = New System.Drawing.Point(24, 256)
- Me.pBarD.Name = "pBarD"
- Me.pBarD.Size = New System.Drawing.Size(448, 25)
- Me.pBarD.Style = System.Windows.Forms.ProgressBarStyle.Continuous
- Me.pBarD.TabIndex = 5
- '
- 'Label2
- '
- Me.Label2.AutoSize = True
- Me.Label2.ForeColor = System.Drawing.Color.White
- Me.Label2.Location = New System.Drawing.Point(265, 218)
- Me.Label2.Name = "Label2"
- Me.Label2.Size = New System.Drawing.Size(69, 15)
- Me.Label2.TabIndex = 7
- Me.Label2.Text = "Completed:"
- '
- 'lblDownloadedBytesD
- '
- Me.lblDownloadedBytesD.BackColor = System.Drawing.Color.FromArgb(CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer))
- Me.lblDownloadedBytesD.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
- Me.lblDownloadedBytesD.ForeColor = System.Drawing.Color.Green
- Me.lblDownloadedBytesD.Location = New System.Drawing.Point(340, 213)
- Me.lblDownloadedBytesD.Name = "lblDownloadedBytesD"
- Me.lblDownloadedBytesD.Size = New System.Drawing.Size(168, 24)
- Me.lblDownloadedBytesD.TabIndex = 6
- Me.lblDownloadedBytesD.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
- '
- 'txtFilePathD
- '
- Me.txtFilePathD.BackColor = System.Drawing.Color.Gainsboro
- Me.txtFilePathD.ForeColor = System.Drawing.Color.Black
- Me.txtFilePathD.Location = New System.Drawing.Point(24, 96)
- Me.txtFilePathD.Name = "txtFilePathD"
- Me.txtFilePathD.Size = New System.Drawing.Size(484, 23)
- Me.txtFilePathD.TabIndex = 8
- Me.txtFilePathD.Text = "ftp://185.201.11.24/Blender2.blend"
- '
- 'Label4
- '
- Me.Label4.AutoSize = True
- Me.Label4.ForeColor = System.Drawing.Color.White
- Me.Label4.Location = New System.Drawing.Point(21, 20)
- Me.Label4.Name = "Label4"
- Me.Label4.Size = New System.Drawing.Size(68, 15)
- Me.Label4.TabIndex = 9
- Me.Label4.Text = "User Name:"
- '
- 'Label5
- '
- Me.Label5.AutoSize = True
- Me.Label5.ForeColor = System.Drawing.Color.White
- Me.Label5.Location = New System.Drawing.Point(274, 20)
- Me.Label5.Name = "Label5"
- Me.Label5.Size = New System.Drawing.Size(60, 15)
- Me.Label5.TabIndex = 10
- Me.Label5.Text = "Password:"
- '
- 'Label6
- '
- Me.Label6.AutoSize = True
- Me.Label6.ForeColor = System.Drawing.Color.White
- Me.Label6.Location = New System.Drawing.Point(21, 78)
- Me.Label6.Name = "Label6"
- Me.Label6.Size = New System.Drawing.Size(115, 15)
- Me.Label6.TabIndex = 11
- Me.Label6.Text = "Download Resource:"
- '
- 'Label3
- '
- Me.Label3.AutoSize = True
- Me.Label3.ForeColor = System.Drawing.Color.White
- Me.Label3.Location = New System.Drawing.Point(21, 132)
- Me.Label3.Name = "Label3"
- Me.Label3.Size = New System.Drawing.Size(97, 15)
- Me.Label3.TabIndex = 13
- Me.Label3.Text = "Destination Path:"
- '
- 'txtSavePathD
- '
- Me.txtSavePathD.BackColor = System.Drawing.Color.Gainsboro
- Me.txtSavePathD.ForeColor = System.Drawing.Color.Black
- Me.txtSavePathD.Location = New System.Drawing.Point(24, 150)
- Me.txtSavePathD.Name = "txtSavePathD"
- Me.txtSavePathD.Size = New System.Drawing.Size(448, 23)
- Me.txtSavePathD.TabIndex = 12
- '
- 'lblPercentD
- '
- Me.lblPercentD.ForeColor = System.Drawing.Color.White
- Me.lblPercentD.Location = New System.Drawing.Point(472, 253)
- Me.lblPercentD.Name = "lblPercentD"
- Me.lblPercentD.Size = New System.Drawing.Size(41, 28)
- Me.lblPercentD.TabIndex = 14
- Me.lblPercentD.Text = "0%"
- Me.lblPercentD.TextAlign = System.Drawing.ContentAlignment.MiddleRight
- '
- 'btnAbortDownload
- '
- Me.btnAbortDownload.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
- Me.btnAbortDownload.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
- Me.btnAbortDownload.FlatStyle = System.Windows.Forms.FlatStyle.Flat
- Me.btnAbortDownload.Location = New System.Drawing.Point(155, 305)
- Me.btnAbortDownload.Name = "btnAbortDownload"
- Me.btnAbortDownload.Size = New System.Drawing.Size(111, 38)
- Me.btnAbortDownload.TabIndex = 15
- Me.btnAbortDownload.Text = "Abort Download"
- Me.btnAbortDownload.UseVisualStyleBackColor = True
- '
- 'btnOpenPath
- '
- Me.btnOpenPath.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
- Me.btnOpenPath.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
- Me.btnOpenPath.FlatStyle = System.Windows.Forms.FlatStyle.Flat
- Me.btnOpenPath.Font = New System.Drawing.Font("Segoe UI Symbol", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.btnOpenPath.Location = New System.Drawing.Point(478, 151)
- Me.btnOpenPath.Name = "btnOpenPath"
- Me.btnOpenPath.Size = New System.Drawing.Size(30, 22)
- Me.btnOpenPath.TabIndex = 16
- Me.btnOpenPath.Text = "⋯"
- Me.btnOpenPath.UseVisualStyleBackColor = True
- '
- 'frmBGWorkerDownload
- '
- Me.AutoScaleDimensions = New System.Drawing.SizeF(96.0!, 96.0!)
- Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
- Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer))
- Me.ClientSize = New System.Drawing.Size(527, 364)
- Me.Controls.Add(Me.btnOpenPath)
- Me.Controls.Add(Me.btnAbortDownload)
- Me.Controls.Add(Me.lblPercentD)
- Me.Controls.Add(Me.Label3)
- Me.Controls.Add(Me.txtSavePathD)
- Me.Controls.Add(Me.Label6)
- Me.Controls.Add(Me.Label5)
- Me.Controls.Add(Me.Label4)
- Me.Controls.Add(Me.txtFilePathD)
- Me.Controls.Add(Me.Label2)
- Me.Controls.Add(Me.lblDownloadedBytesD)
- Me.Controls.Add(Me.pBarD)
- Me.Controls.Add(Me.Label1)
- Me.Controls.Add(Me.lblFileSizeD)
- Me.Controls.Add(Me.txtFTPPasswordD)
- Me.Controls.Add(Me.txtFTPUsernameD)
- Me.Controls.Add(Me.btnDownload)
- Me.Font = New System.Drawing.Font("Segoe UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.ForeColor = System.Drawing.Color.White
- Me.Name = "frmBGWorkerDownload"
- Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
- Me.Text = "Ftp Download with BackGroundWorker"
- Me.ResumeLayout(False)
- Me.PerformLayout()
- End Sub
- Friend WithEvents btnDownload As Button
- Friend WithEvents txtFTPUsernameD As TextBox
- Friend WithEvents txtFTPPasswordD As TextBox
- Friend WithEvents lblFileSizeD As Label
- Friend WithEvents Label1 As Label
- Friend WithEvents pBarD As ProgressBar
- Friend WithEvents Label2 As Label
- Friend WithEvents lblDownloadedBytesD As Label
- Friend WithEvents txtFilePathD As TextBox
- Friend WithEvents Label4 As Label
- Friend WithEvents Label5 As Label
- Friend WithEvents Label6 As Label
- Friend WithEvents Label3 As Label
- Friend WithEvents txtSavePathD As TextBox
- Friend WithEvents lblPercentD As Label
- Friend WithEvents btnAbortDownload As Button
- Friend WithEvents btnOpenPath As Button
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement