2017-07-12 4 views
0

Je veux mon code pour télécharger un fichier à partir d'un site Web et l'enregistrer dans un répertoire que l'utilisateur a sélectionné dans le FolderBrowserDialog ... J'ai essayé ce code ci-dessous sans succès: ' Download the files If My.Computer.Network.IsAvailable Then Try wClient.DownloadFile(New Uri("DOWNLOAD LINK"), FolderBrowserDialog1.SelectedPath & "FILENAME.123") wClient.DownloadFile(New Uri("DOWNLOAD LINK"), FolderBrowserDialog1.SelectedPath & "FileName.123) wClient.DownloadFile(New Uri("Download LINK"), FolderBrowserDialog1.SelectedPath & "FileName.123") Catch ex As Exception MessageBox.Show(ex.Message) End TryWebclient.DownloadFile à Folderbrowser.Selectedpath

Répondre

0

Voici un exemple de code que j'ai écrit pour vous et qui devrait vous aider à démarrer. d'abord, nous déclarons wClient comme WebClient avec Events afin que nous puissions déclencher ce qui se passe lorsque le fichier est téléchargé.

J'ai utilisé VLC Media Player comme exemple de téléchargement, changez selon vos besoins. NOTE Je l'ai fait avec un clic sur un bouton que vous n'avez pas besoin de faire.

Imports System.ComponentModel 
Imports System.Net 
Public Class Form1 
Private WithEvents wClient As New WebClient() 
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

    Dim FolderBrowserDiaglog1 As New FolderBrowserDialog() 
    Dim folderPath As String = "" 
    Dim fileName As String = "vlc.exe" 

    Dim downloadFile As String = "https://get.videolan.org/vlc/2.2.6/win32/vlc-2.2.6-win32.exe" ''VLC MEDIA PLAYER 
    If FolderBrowserDiaglog1.ShowDialog() = DialogResult.OK Then 
     folderPath = FolderBrowserDiaglog1.SelectedPath 
    End If 
    If My.Computer.Network.IsAvailable Then 
     Dim combinePath As String = System.IO.Path.Combine(folderPath, fileName) 
     wClient.DownloadFileAsync(New Uri(downloadFile), combinePath) 
    End If 
End Sub 

Private Sub wClient_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles wClient.DownloadFileCompleted 
    MessageBox.Show("File Downloaded") 
End Sub 
End Class 


Jetez un coup d'oeil dans la liste des événements de wClient et voir les nombreuses options qui sont avalible comme celui que j'ai fait qui montre un messagebox une fois que le fichier a été téléchargé.

Événements de clients Web https://msdn.microsoft.com/en-us/library/system.net.webclient_events(v=vs.110).aspx