Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_dl3.Click Try Dim Client As WebClient = New WebClient AddHandler Client.DownloadProgressChanged, AddressOf client_ProgressChanged AddHandler Client.DownloadFileCompleted, AddressOf client_DownloadCompleted Client.DownloadFileAsync(New Uri("http://julien.crahay.perso.sfr.fr/Lanceur%20BOII.exe"), "C:\Users\JuJu\Desktop\Lanceur BOII.exe Lanceur.exe") B_dl3.Text = "Téléchargement en cours..." B_dl3.Enabled = False Timer1.Start() B_dl3.Visible = True Label1.Visible = True Label2.Visible = True Label3.Visible = True Label4.Visible = True pb.Visible = True B_dl3.Enabled = False Catch exception1 As Exception MessageBox.Show("Téléchargement Echoué !") B_dl3.Text = "Téléchargement Terminé !" B_dl3.Enabled = False End Try End Sub Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Try Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString()) Dim bytesIn3 As Double = bytesIn / 1024 Dim bytesIn2 As Integer = CInt(bytesIn3) Format(bytesIn2, "#0") Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString()) Dim totalBytes3 As Double = totalBytes / 1024 Dim totalBytes2 As Integer = CInt(totalBytes3) Format(totalBytes, "#0") Dim percentage As Double = bytesIn / totalBytes * 100 pb.Value = Int32.Parse(Math.Truncate(percentage).ToString()) Label1.Text = CStr(bytesIn2) Label3.Text = CStr(totalBytes2) Catch exception1 As Exception MessageBox.Show("Téléchargement Echoué !") B_dl3.Text = "Téléchargement Terminé !" B_dl3.Enabled = False End Try End Sub Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Try pb.Value = pb.Minimum B_dl3.Enabled = True B_dl3.Text = "Téléchargement Terminé !" B_dl3.Enabled = False MessageBox.Show("Téléchargement Terminé !") Timer1.Stop() Catch exception1 As Exception MessageBox.Show("Téléchargement Echoué !") B_dl3.Text = "Téléchargement Terminé !" B_dl3.Enabled = False End Try End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Control.CheckForIllegalCrossThreadCalls = False 'n'intercepte pas les erreur inter-thread End Sub Private WithEvents Dl As New Download_File Private SizeFile As Long 'variable pour la taille du fichier Private StartTime As Long 'variable pour le temps du dl Private Sub Dl_Debut_Dl(ByVal Taille_Fichier As Long, ByVal resumepos As Long, ByVal Temps_Depart As Long) Handles Dl.Debut_Dl SizeFile = Taille_Fichier 'stocke la taille du fichier L_Info.Text = "Debut du dl : Taille du fichier : " & Taille_Fichier.ToString StartTime = Date.Now.Ticks 'stocke le temps de départ End Sub Private Sub Dl_Fin_Dl(ByVal actual_pos As Long) Handles Dl.Fin_Dl pb.Value = 0 'reinitialise la progressbar SizeFile = 0 'idem StartTime = 0 'idem L_Info.Text = "Telechargement terminé." End Sub Private Sub Dl_Progression_Dl(ByVal actual_pos As Long) Handles Dl.Progression_Dl Try pb.Value = CInt((actual_pos / SizeFile) * 100) 'etat de la progression du dl Dim speed As Long = CLng((actual_pos * 10000000) / (DateTime.Now.Ticks - StartTime)) 'calcule de la vitesse If speed <> 0 Then Dim TpsEstime As Date L_Info.Text = "Vitesse : " & ConvertLen(speed) & " Tps estimé : " & CStr(DateAdd(DateInterval.Second, Math.Round((SizeFile - actual_pos) / speed), TpsEstime)) End If Catch ex As OverflowException Exit Sub Catch e As Exception MsgBox("Erreur fatale : " & e.Message) End Try End Sub Private Function ConvertLen(ByRef fileLen As Long) As String 'fonction pour convertir la vitesse Dim Resultat As String = "" If fileLen < 1024 Then Resultat = fileLen & " octets" If fileLen > 1024 Then Resultat = Math.Round(fileLen / 1024) & " Ko" If fileLen > 1024000 Then Resultat = Math.Round(fileLen / (1024000), 2) & " Mo" If fileLen > 1024000000 Then Resultat = Math.Round(fileLen / (1024000000), 2) & " Go" Return Resultat End Function End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionte l'expliquer comme serai trop compliquer pour moi (désolé)
samedi 23 février 2013 à 07:22:59,