本文介绍了Webclient.DownloadFile到Folderbrowser.Selectedpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的代码从网站上下载文件并将其保存到用户在FolderBrowserDialog中选择的目录...我在下面尝试了以下代码,但未成功:
下载文件
如果My.Computer.Network.IsAvailable然后
尝试
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)
作为例外捕获
MessageBox.Show(ex.Message)
End Try

I want my code to download a file from a website and save it to an directory that the user has selected in the FolderBrowserDialog ... i've tried this code below without success:' 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 作为具有事件 WebClient ,因此我们可以触发文件下载时发生的情况。

我已使用VLC Media Player作为示例下载,可以进行更改以满足您的需求。注意我是通过按钮单击事件来完成此操作的,您不必这样做。

Here is some sample code I have written for you that should get you started.first we declare wClient as a WebClient with Events so we can trigger what happens when the file downloads.

I have used VLC Media Player as an example download, change to suit your needs. NOTE I did this with a button click event which you don't necessary need to do.

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 的事件列表中查看,并看到许多可用的选项,例如


Webclient事件


Have a look in the wClient's event list and see the many options that are avalible such as the one that i have made that shows a messagebox once the file has been downloaded.

Webclient events https://msdn.microsoft.com/en-us/library/system.net.webclient_events(v=vs.110).aspx

这篇关于Webclient.DownloadFile到Folderbrowser.Selectedpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 11:46