yukarıdaki resimde gördüğünüz benim kendi yerleştirme stilim siz istediğiniz gibi yapın
label1 in texti: Dosyanın İndirileceği Adres:
label2 nşn texti: Kayıt Yeri:
label3: İsim:
label4: Down. Adres
label5: Kayıt Yeri:
label6: Boyut:
label7: Hız:
label8: İndirilen:
Label9: 0%
button1 in texti indir, button2 nin ise iptal. dosya butonu ise button3
şimdi kodaları veriyorum. açılaması yok fakat kolay anlaşılabilicek kodlar.
[BILGI]Imports System.Net
Public Class Form1
Dim whereToSave As String
Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
Public Sub DownloadComplete(ByVal cancelled As Boolean)
Me.TextBox1.Enabled = True
Me.Button1.Enabled = True
If cancelled Then
Me.Button2.Enabled = False
Me.Label8.Text = "İndirilen : " & "İptal"
MessageBox.Show("İndirmeyi İptal Edin.", "İptal", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Me.Button2.Enabled = False
Me.Label8.Text = "İndirilen : " & "Başarıyla İndirildi"
MessageBox.Show("Başarıyla İndirildi !", "İndirme Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Button3.Enabled = True
Me.TextBox2.Enabled = True
End If
Me.ProgressBar1.Value = 0
End Sub
Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Me.Label6.Text = "Boyut : " & Math.Round((length / 1024), 2) & " KB"
Me.Label4.Text = "Down. Adres : " & Me.TextBox1.Text
Me.Label8.Text = "İndirilen : " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
Me.Label9.Text = Me.ProgressBar1.Value & "%"
If speed = -1 Then
Me.Label7.Text = "Hız : " & "calculating..."
Else
Me.Label7.Text = "Hız : " & Math.Round((speed / 1024), 2) & " KB/s"
End If
Me.ProgressBar1.Value = percent
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox1.Text <> "" AndAlso Me.TextBox1.Text.StartsWith("http://") Then
Me.whereToSave = Me.TextBox2.Text
Me.SaveFileDialog1.FileName = ""
Me.Label5.Text = "Save : " & whereToSave
Me.TextBox1.Enabled = False
Me.Button1.Enabled = False
Me.Button2.Enabled = True
Me.TextBox2.Enabled = False
Me.Button3.Enabled = False
Me.BackgroundWorker1.RunWorkerAsync()
Else
MessageBox.Show("Url Alanı Boş :", "DİKKAT!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.SaveFileDialog1.FileName = Me.TextBox1.Text.Split("/"c)(Me.TextBox1.Text.Split("/"c).Length - 1)
Me.Label3.Text = "İsim : " & Me.TextBox1.Text.Split("/"c)(Me.TextBox1.Text.Split("/"c).Length - 1)
Me.SaveFileDialog1.ShowDialog()
Me.TextBox2.Text = Me.SaveFileDialog1.FileName
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Me.Button1.Enabled = False
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Try
theRequest = WebRequest.Create(Me.TextBox1.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
MessageBox.Show("Hata" & ControlChars.CrLf & _
"1) Sunucuyla Bağlantı Kurulamıyor." & ControlChars.CrLf & _
"2) Url Adresi yanlış olabilir.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Long = theResponse.ContentLength
Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
Me.Invoke(safedelegate, length, 0, 0, 0)
Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
Dim nRead As Integer
Dim speedtimer As New Stopwatch
Dim currentspeed As Double = -1
Dim readings As Integer = 0
Do
If BackgroundWorker1.CancellationPending Then
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 / length) * 100
Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
speedtimer.Stop()
readings += 1
If readings >= 5 Then
currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
speedtimer.Reset()
readings = 0
End If
Loop
theResponse.GetResponseStream.Close()
writeStream.Close()
If Me.BackgroundWorker1.CancellationPending Then
IO.File.Delete(Me.whereToSave)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(completeDelegate, False)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.TextBox1.Text <> "" AndAlso Me.TextBox1.Text.StartsWith("http://") Then
Me.Label5.Text = "Save : " & whereToSave
Me.TextBox1.Enabled = False
Me.Button1.Enabled = True
Me.Button2.Enabled = False
End
End If
End Sub[/BILGI]
kodların çoğu alıntıdır. vb si kötü olan biri buradaki kodları incelerse neyin ne için kullanıldığını anlaycaktır. ben bilmediğim şeyleri bunu sayesinde öğrendim.
kodları @jojomax31 ın isteği üzerine verdim.
iyi çalışmalar