Once Bir UserControl Ekleyin Adını Yani Name Kısmını Downloader Yazın Ve Aşağıdaki Kodları Ekleyip Sonrada Projeyi kaydedip yeniden Acın.
Option Explicit
Event DownloadProgress(CurBytes As Long, MaxBytes As Long, SaveFile As String)
Event DownloadError(SaveFile As String)
Event DownloadComplete(MaxBytes As Long, SaveFile As String)
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
On Error Resume Next
Dim F() As Byte, Fn As Long
If AsyncProp.BytesMax 0 Then
Fn = FreeFile
F = AsyncProp.Value
Open AsyncProp.PropertyName For Binary Access Write As #Fn
Put #Fn, , F
Close #Fn
Else
RaiseEvent DownloadError(AsyncProp.PropertyName)
End If
RaiseEvent DownloadComplete(CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
On Error Resume Next
If AsyncProp.BytesMax 0 Then
RaiseEvent DownloadProgress(CLng(AsyncProp.BytesRead), CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
End If
End Sub
Private Sub UserControl_Resize()
UserControl.Width = ScaleX(32, vbPixels, vbTwips)
UserControl.Height = ScaleY(32, vbPixels, vbTwips)
End Sub
Public Sub BeginDownload(URL As String, SaveFile As String)
On Error GoTo ErrorBeginDownload
UserControl.AsyncRead URL, vbAsyncTypeByteArray, SaveFile, vbAsyncReadForceUpdate
Exit Sub
ErrorBeginDownload:
RaiseEvent DownloadError(SaveFile)
Exit Sub
End Sub
Kullanımı :
Actıktan Sonra Form'a Gelip Download Usercontrol'unu Ekleyelim
Private Sub Downloader1_DownloadComplete(MaxBytes As Long, SaveFile As String)
'indirme Tamamlandığında Yapılacak Işlemler
End Sub
Private Sub Downloader1_DownloadProgress(CurBytes As Long, MaxBytes As Long, SaveFile As String)
'Progressbar Varsa'
ProgressBar1.Max = MaxBytes
ProgressBar1.Value = CurBytes
'Indirme Bilgileri'
CurBytes - Indirilen Mb
MaxBytes - Toplam Mb
Eğer Kalan Mb'yi Bulmak Istiyorsanız
maxbytes - curbytes
yani maximumdan normal değeri cıkarın size gosterir
End Sub
'Indirme Kodu'
Downloader1.BeginDownload ("Dosyanın Url'si","Indirilecek Dosya Konumu")
Visual Basic .Net Icin:
Yeni Bir Class Ekleyin Ctrl+A basıp Silin Hepsini Ve Aşağıdaki Kodları Yazın...
Imports System.Net.WebClient
Public Class DownloadClass
Event DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
Event DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Public WithEvents Download As New System.Net.WebClient
Public Sub CancelDownload()
Download.CancelAsync()
End Sub
Public Sub DownloadFile(ByVal Url As String, ByVal FileName As String)
Download.DownloadFileAsync(New Uri(Url), FileName)
End Sub
Private Sub Download_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles Download.DownloadFileCompleted
RaiseEvent DownloadFileCompleted(sender, e)
End Sub
Private Sub Download_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles Download.DownloadProgressChanged
RaiseEvent DownloadProgressChanged(sender, e)
End Sub
End Class
Kullanımı :
Formun Public Class Kısmın Altına Şu Kodu Ekleyiniz...
public withevents Download as new DownloadClass
'Indirme Kodu :'
Download.DownloadFile("Url", "Indirilecek Dosya Konumu")
'Indirme Iptal Etme Kodu :'
Download.CancelDownload()
'Indirme Bilgilerini Almak İcin ise'
Private Sub Download_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles Download.DownloadFileCompleted
'Indirme Tamamlandı Kodları
End Sub
Private Sub Download_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles Download.DownloadProgressChanged
'Progressbar icin
progressbar1.maximum = e.TotalBytesToReceive
progressbar1.value = e.BytesReceived
'Indirilen Mb Gosterme
Dim indirilen, indirilecek, kalan, yuzde
indirilen = Math.Round(e.BytesReceived / 1024 / 1024, 2) & " Mb"
indirilecek = Math.Round(e.TotalBytesToReceive / 1024 / 1024, 2) & " Mb"
kalan = Math.Round(e.TotalBytesToReceive / 1024 / 1024, 2) - Math.Round(e.BytesReceived / 1024 / 1024, 2) & " MB"
yuzde = e.ProgressPercentage & "%"
'mesela bir deneme
lblindirilen.text = indirilen
lblindirilecek.text = indirilecek
lblkalan.text = kalan
lblyuzde.text = yuzde
End Sub
+replerinizi Bekliyorum...



__________________