Internet Download Manager Vb.NET
Langsung saja, silahkan buka aplikasi visual studio kamu, boleh menggunakan visual studio 2010. dalam tutorial ini penulis membuat aplikasi IDM menggunakan vs 2015. Buatlah project baru dengan nama "InternetDownloadManager" . Pada form1.vb boleh kamu rubah namanya sesuai kebutuhan, serta desainlah tampilan form1 dengan tampilan seperti gambar dibawah ini :Tambahkan juga component BackgroundWorker dan ProgressBar pada form1. jika sudah mari kita mualai berpusing rianya.
Code VB.NET Form1_Load
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadBerfungsi untuk mengosongkan tulisan label6.text saat aplikasi load.
Label6.Text = ""
End Sub
Code VB.NET tombol Download (Button1)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickSelanjutnya letakkan variable berikut dibawah class form1
If Me.TextBox1.Text <> "" AndAlso Me.TextBox1.Text.StartsWith("http://") Then
'we will create the save file dialog here
Me.SaveFileDialog1.FileName = Me.TextBox1.Text.Split("/"c)(Me.TextBox1.Text.Split("/"c).Length - 1)
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.wheretosave = Me.SaveFileDialog1.FileName
Me.SaveFileDialog1.FileName = ""
Label3.Text = "Save to : " & Me.wheretosave
Me.TextBox1.Enabled = False
Me.Button1.Enabled = False
Me.Button2.Enabled = True
Me.BackgroundWorker1.RunWorkerAsync() ' start our download
End If
Else
MessageBox.Show("Warning : Please insert valid URL for download", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Dim wheretosave As String ''Where the program save the file
Delegate Sub DownloadComplateSafe(ByVal cancelled As Boolean)
Delegate Sub ChangeTextSafe(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Code BackgroundWorker
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' creating the request and getting the response
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Try 'check if the file is exist
theRequest = WebRequest.Create(Me.TextBox1.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf &
"1) File doesn't exist" & ControlChars.CrLf &
"2) Remote server error", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
'we will create the delegate here
' just wait fo a moment
Dim cancelDelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim lenght As Long = theResponse.ContentLength 'Size of the response (in bytes)
' we will create the functions for update the informations
' just wait for a moment
Dim safedelegate As New ChangeTextSafe(AddressOf ChangeText)
Me.Invoke(safedelegate, lenght, 0, 0, 0)
Dim writestream As New IO.FileStream(Me.wheretosave, IO.FileMode.Create)
'Replacement for Stream.Position (webResponse stream doesn't support seek)
Dim nRead As Integer
'To calculate the download speed
Dim speedTimer As New Stopwatch
Dim currentspeed As Double = -1
Dim readings As Integer = 0
Do
If BackgroundWorker1.CancellationPending Then 'If user abort download
Exit Do
End If
speedTimer.Start()
Dim readBytes(4095) As Byte
Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Short = (nRead * 100) / lenght
Me.Invoke(safedelegate, lenght, nRead, percent, currentspeed)
' sorry for it, just replace the variable speed to double
' lets try it again
If bytesread = 0 Then Exit Do
writestream.Write(readBytes, 0, bytesread)
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
'Close the streams
theResponse.GetResponseStream.Close()
writestream.Close()
If Me.BackgroundWorker1.CancellationPending Then
IO.File.Delete(Me.wheretosave)
Dim canceldelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
Me.Invoke(canceldelegate, True)
Exit Sub
End If
Dim complatedelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
Me.Invoke(complatedelegate, False)
End Sub
Functions ChangeText
Public Sub ChangeText(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Me.Label4.Text = "File Size: " & Math.Round((lenght / 1024), 2) & " KB"
Me.Label2.Text = "Downloading: " & Me.TextBox1.Text
Me.Label6.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB OF " & Math.Round((lenght / 1024), 2) & " KB (" & ProgressBar1.Value & " %) "
If speed = -1 Then
Me.Label5.Text = "Speed : Calculating ..."
Else
Me.Label5.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
End If
Me.ProgressBar1.Value = percent
End Sub
Functions Download Complate
Public Sub DownloadComplate(ByVal cancelled As Boolean)
Me.TextBox1.Enabled = True
Me.Button1.Enabled = True
Me.Button2.Enabled = False
If cancelled Then
Label6.Text = "Cancelled"
MessageBox.Show("Download Aborted", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Label6.Text = "Download Successfully"
MessageBox.Show("Donload Complated", "All Ok", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Me.ProgressBar1.Value = 0
Me.Label2.Text = "Downloading :"
Me.Label3.Text = "Safe to :"
Me.Label4.Text = "File Size :"
Me.Label5.Text = "Download Speed :"
Me.Label6.Text = ""
End Sub
Code Button Cancel
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickUntuk keseluruhan Source code simple Download Manager menggunakan bahasa pemrograman vb.net silahkan copy code berikut :
Me.BackgroundWorker1.CancelAsync() ' its for send cancel request
' lets try it
End Sub
Source code aplikasi Internet Download Manager lengkap
Imports System.NetAplikasi Download manager diatas dikhususkan hanya untuk kamu yang masih pemula dalam belajar bahasa pemrograman visual basic net. jika masih bingung bisa melihat video tutorialnya langsung di youtube atau dibawah ini :
Public Class Form1
Dim wheretosave As String ''Where the program save the file
Delegate Sub DownloadComplateSafe(ByVal cancelled As Boolean)
Delegate Sub ChangeTextSafe(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label6.Text = ""
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.TextBox1.Text <> "" AndAlso Me.TextBox1.Text.StartsWith("http://") Then
'we will create the save file dialog here
Me.SaveFileDialog1.FileName = Me.TextBox1.Text.Split("/"c)(Me.TextBox1.Text.Split("/"c).Length - 1)
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.wheretosave = Me.SaveFileDialog1.FileName
Me.SaveFileDialog1.FileName = ""
Label3.Text = "Save to : " & Me.wheretosave
Me.TextBox1.Enabled = False
Me.Button1.Enabled = False
Me.Button2.Enabled = True
Me.BackgroundWorker1.RunWorkerAsync() ' start our download
End If
Else
MessageBox.Show("Warning : Please insert valid URL for download", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' creating the request and getting the response
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Try 'check if the file is exist
theRequest = WebRequest.Create(Me.TextBox1.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf &
"1) File doesn't exist" & ControlChars.CrLf &
"2) Remote server error", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
'we will create the delegate here
' just wait fo a moment
Dim cancelDelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim lenght As Long = theResponse.ContentLength 'Size of the response (in bytes)
' we will create the functions for update the informations
' just wait for a moment
Dim safedelegate As New ChangeTextSafe(AddressOf ChangeText)
Me.Invoke(safedelegate, lenght, 0, 0, 0)
Dim writestream As New IO.FileStream(Me.wheretosave, IO.FileMode.Create)
'Replacement for Stream.Position (webResponse stream doesn't support seek)
Dim nRead As Integer
'To calculate the download speed
Dim speedTimer As New Stopwatch
Dim currentspeed As Double = -1
Dim readings As Integer = 0
Do
If BackgroundWorker1.CancellationPending Then 'If user abort download
Exit Do
End If
speedTimer.Start()
Dim readBytes(4095) As Byte
Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Short = (nRead * 100) / lenght
Me.Invoke(safedelegate, lenght, nRead, percent, currentspeed)
' sorry for it, just replace the variable speed to double
' lets try it again
If bytesread = 0 Then Exit Do
writestream.Write(readBytes, 0, bytesread)
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
'Close the streams
theResponse.GetResponseStream.Close()
writestream.Close()
If Me.BackgroundWorker1.CancellationPending Then
IO.File.Delete(Me.wheretosave)
Dim canceldelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
Me.Invoke(canceldelegate, True)
Exit Sub
End If
Dim complatedelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
Me.Invoke(complatedelegate, False)
End Sub
Public Sub ChangeText(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Me.Label4.Text = "File Size: " & Math.Round((lenght / 1024), 2) & " KB"
Me.Label2.Text = "Downloading: " & Me.TextBox1.Text
Me.Label6.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB OF " & Math.Round((lenght / 1024), 2) & " KB (" & ProgressBar1.Value & " %) "
If speed = -1 Then
Me.Label5.Text = "Speed : Calculating ..."
Else
Me.Label5.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
End If
Me.ProgressBar1.Value = percent
End Sub
Public Sub DownloadComplate(ByVal cancelled As Boolean)
Me.TextBox1.Enabled = True
Me.Button1.Enabled = True
Me.Button2.Enabled = False
If cancelled Then
Label6.Text = "Cancelled"
MessageBox.Show("Download Aborted", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Label6.Text = "Download Successfully"
MessageBox.Show("Donload Complated", "All Ok", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Me.ProgressBar1.Value = 0
Me.Label2.Text = "Downloading :"
Me.Label3.Text = "Safe to :"
Me.Label4.Text = "File Size :"
Me.Label5.Text = "Download Speed :"
Me.Label6.Text = ""
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.BackgroundWorker1.CancelAsync() ' its for send cancel request
' lets try it
End Sub
End Class
Video tutorial Cara membuat Aplikasi Download Manager
Google Plus : https://plus.google.com/+HarisonMatondang
Youtube : http://www.youtube.com/c/HarisonMatondang
Officel Website : Belajar membuat game android sampai ngehh di ngehh.id
Jangan lupa share dan subscribe juga ya untuk mendapatkan tips dan trik serta tutorial programming terbaru setiap harinya.