Private Function FTPRequestFile() As Boolean Dim ftpURI As String Dim nRead As Integer Dim speedtimer As New Stopwatch Dim currentspeed As Double = -1 Dim readings As Integer = 0 Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts) l_ExtractProgress.Text = "Preparing to download ..." Try ftpURI = "ftp://" & ConfigX.FTPServer & "/" & ConfigX.FTPFilePath & "/" & ConfigX.SaveFileName Dim FTPRequest As FtpWebRequest = FtpWebRequest.Create(ftpURI) With FTPRequest .EnableSsl = False .Credentials = New NetworkCredential(ConfigX.FTPUser, ConfigX.FTPPassword) .KeepAlive = True .UseBinary = True .UsePassive = True .Timeout = Timeout.Infinite .Method = System.Net.WebRequestMethods.Ftp.DownloadFile End With Using FTPResponse As System.Net.FtpWebResponse = CType(FTPRequest.GetResponse, System.Net.FtpWebResponse) Using responseStream As IO.Stream = FTPResponse.GetResponseStream Dim length As Long = FTPResponse.ContentLength 'Size of the response (in bytes) Using fs As New IO.FileStream(ConfigX.SaveFilePath + ConfigX.SaveFileName, IO.FileMode.Create) Dim buffer(2047) As Byte Dim read As Integer = 0 Do If BackgroundWorker1.CancellationPending Then 'If user abort download Exit Do End If speedtimer.Start() Dim readBytes(4095) As Byte read = responseStream.Read(buffer, 0, buffer.Length) nRead += read Dim percent As Short = (nRead * 100) / length Me.Invoke(safedelegate, length, nRead, percent, currentspeed) If read = 0 Then Exit Do fs.Write(buffer, 0, read) speedtimer.Stop() readings += 1 If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000) speedtimer.Reset() readings = 0 End If Loop Until read = 0 responseStream.Close() fs.Flush() fs.Close() End Using responseStream.Close() End Using FTPResponse.Close() End Using Return True Catch ex As Exception Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete) Me.Invoke(cancelDelegate, True) Return False End Try End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)