Imports System.Threading
Public Class Form1
Delegate Sub down(ByVal url As String)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim t As New Thread(New ThreadStart(Sub() download("mon url")))
t.Start()
End Sub
Private Sub download(ByVal url As String)
If InvokeRequired Then
Invoke(New down(AddressOf download), url)
Else
My.Computer.Network.DownloadFile(url, "c:\temp\test.mp3")
End If
End Sub
End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Imports System.Threading
Imports System.Net
Public Class Form1
Dim tailleFichftp
Dim fichlocal
Dim fichftp
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim user = ""
Dim pass = ""
fichlocal = "" 'Liens du fichier local
fichftp = "" 'lien du fichier ftp
Try
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(fichftp), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.GetFileSize
request.Credentials = New NetworkCredential(user, pass)
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
tailleFichftp = response.ContentLength
Catch ex As Exception
End Try
Dim dl As New Thread(New ThreadStart(Sub() download(fichftp, fichlocal, user, pass)))
dl.Start()
Timer1.Start()
End Sub
Private Sub download(ByVal fichftp As String, ByVal fichlocal As String, ByVal user As String, ByVal pass As String)
Try
My.Computer.Network.DownloadFile(fichftp, fichlocal, user, pass)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim info As New System.IO.FileInfo(fichlocal)
Dim tailleFichlocal As Long = info.Length
Dim progress = (tailleFichlocal * 100) / tailleFichftp
ProgressBar1.Value = progress
If ProgressBar1.Value = 100 Then
Timer1.Stop()
ProgressBar1.Value = 0
End If
End Sub
End Class
Public Class Form1
Dim wc As System.Net.WebClient
Dim url As String = ""
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
wc = New System.Net.WebClient()
AddHandler wc.DownloadProgressChanged, AddressOf OnDownloadProgressChanged
AddHandler wc.DownloadFileCompleted, AddressOf OnFileDownloadCompleted
wc.DownloadFileAsync(New Uri("fichier"), "C:\temp\test.mp3")
Label1.Text = "connection au serveur..."
End Sub
Private Sub OnDownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
Dim totalSize As Long = e.TotalBytesToReceive
Dim downloadedBytes As Long = e.BytesReceived
Dim percentage As Integer = e.ProgressPercentage
ProgressBar1.Value = percentage
'Put your progress UI here, you can cancel download by uncommenting the line below
'wc.CancelAsync()
End Sub
Private Sub OnFileDownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If e.Cancelled Then
'Cancelled
ElseIf Not e.Error Is Nothing Then
'Error occured
Else
'File Downloaded Successfuly
End If
End Sub
Dim wc As System.Net.WebClient
Dim url As String = ""
Private Sub btnButton1_Click(sender As Object, e As EventArgs) Handles Button1.Click
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
Dim dl As New Thread(New ThreadStart(Sub() dld()))
dl.Start()
Label1.Text = "connection au serveur..."
End Sub
Sub dld()
wc = New System.Net.WebClient()
AddHandler wc.DownloadProgressChanged, AddressOf OnDownloadProgressChanged
AddHandler wc.DownloadFileCompleted, AddressOf OnFileDownloadCompleted
wc.DownloadFileAsync(New Uri("fichier"), "C:\temp\test.mp3")
End Sub
Private Sub OnDownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
Dim totalSize As Long = e.TotalBytesToReceive
Dim downloadedBytes As Long = e.BytesReceived
Dim percentage As Integer = e.ProgressPercentage
ProgressBar1.Value = percentage
ProgressBar1.Refresh()
'Put your progress UI here, you can cancel download by uncommenting the line below
'wc.CancelAsync()
End Sub
Private Sub OnFileDownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If e.Cancelled Then
'Cancelled
ElseIf Not e.Error Is Nothing Then
'Error occured
Else
'File Downloaded Successfuly
End If
End Sub