【问题标题】:Webclient.DownloadFile to Folderbrowser.SelectedpathWebclient.DownloadFile 到 Folderbrowser.Selectedpath
【发布时间】:2017-12-16 05:53:13
【问题描述】:

我希望我的代码从网站下载文件并将其保存到用户在 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

【问题讨论】:

    标签: vb.net dialog webclient downloadfile


    【解决方案1】:

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

    我使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多