Webclient.DownloadFile 至 Folderbrowser.Selectedpath

Webclient.DownloadFile to Folderbrowser.Selectedpath

我希望我的代码从网站下载文件并将其保存到用户在 FolderBrowserDialog 中选择的目录中......我已经尝试了下面的代码但没有成功: ' 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 Try

这是我为您编写的一些示例代码,可以帮助您入门。 首先我们将 wClient 声明为带有 EventsWebClient 这样我们就可以触发文件下载时发生的事情。

我使用 VLC 媒体播放器作为示例下载,更改以满足您的需要。注意我用一个你不需要做的按钮点击事件来做到这一点。

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


查看 wClient 的事件列表并查看许多可用的选项,例如我制作的在文件下载后显示消息框的选项.

Web 客户端事件 https://msdn.microsoft.com/en-us/library/system.net.webclient_events(v=vs.110).aspx