Pernah berfikir untuk membuat sebuah Download Manager Seperti IDM atau lainya? Dan disini lah tempat anda beljar membuat aplikasi seperti itu. Di artikel kali ini aku akan share tentang bagaimana cara membuat sebuah Download Manager menggunakan VB.NET . Kecepat download filenya pun saya coba sudah lumayan bagus dan hampir sama dengan download manager di firefox atau di crome. untuk membuatnya ikuti langkah berikut :
1] Buat Sebuah Project Baru
Buat project di Visual Studio mu dengan cara menekan menu File > New Project. atau anda bisa meneruskan project mu yang lain dengan cara File > Open Project lalu ke Project > New Windows Form.
2] Tata Letak Form
Gunakan tata letak diatas sebagai tata letak desain form mu, list toolbox yang digunakan adalah :
*Label1 sampai Label6
*1 Textbox
* 1 Progress bar
*SaveFile Dialog
*Background Worker
* 2 Button
semua componet dengan properti : Name => Default
3] Masukan Code
klik 2 kali pada area form atau klik kanan lalu "View Code". masukan code berikut :
Imports
System.Net
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
4] Lakukan uji coba
tekan F5 utun melakukan Start Debug.
Jika ada yang ditanyaka silahkan di komentar atau kontak saya.
viandwicyber@gmail.com
keyword :
membuat
internedt download manager di vbnet, source code idm vbnet, membuat
download manager di vb.net, idm di vbn.net, cara membuat aplikasi idm,
membuat download file di vb.net, download file lewat vbnet, download
file in vb.net, create application download manager in vb.net.