I am trying to make a download client with ftp and im still trying to find out whats the best i have but im in stuck because i followed a guide but it only show how much its have downloaded in bytes, how can i change it to gb? and how can i add a download speed?
Code:Imports System.Net Public Class MainForm Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load Control.CheckForIllegalCrossThreadCalls = False End Sub Private Sub btnBrowseSave_Click(sender As Object, e As EventArgs) Handles btnBrowseSave.Click Dim newFolder As New FolderBrowserDialog If newFolder.ShowDialog = Windows.Forms.DialogResult.OK Then txtSavePath.Text = newFolder.SelectedPath End If End Sub Private Sub btnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click bWorker.RunWorkerAsync() End Sub Private Sub bWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bWorker.DoWork Dim buffer(1023) As Byte Dim bytesIn As Integer Dim totalBytesIn As Integer Dim output As IO.Stream Dim flLength As Integer Try Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest) FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text) FTPRequest.Method = Net.WebRequestMethods.Ftp.GetFileSize flLength = CInt(FTPRequest.GetResponse.ContentLength) lblFileSize.Text = flLength & " bytes" Catch ex As Exception End Try Try Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest) FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text) FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream Dim OutputFilePath As String = txtSavePath.Text & "\" & IO.Path.GetFileName(txtFilePath.Text) output = System.IO.File.Create(OutputFilePath) bytesIn = 1 Do Until bytesIn < 1 bytesIn = stream.Read(buffer, 0, 1024) If bytesIn > 0 Then output.Write(buffer, 0, bytesIn) totalBytesIn += bytesIn lblDownloadedBytes.Text = totalBytesIn.ToString & " bytes" If flLength > 0 Then Dim perc As Integer = (totalBytesIn / flLength) * 100 bWorker.ReportProgress(perc) End If End If Loop output.Close() stream.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged pBar.Value = e.ProgressPercentage lblPercent.Text = e.ProgressPercentage.ToString & "%" End Sub Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted MsgBox("Download Complete!") End Sub End Class




Reply With Quote
