我希望我的代码从网站下载文件并将其保存到用户在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
答案 0 :(得分:0)
以下是我为您编写的一些示例代码,可以帮助您入门。
首先我们将wClient
声明为WebClient
Events
,以便我们可以触发文件下载时发生的情况。
我使用VLC Media Player作为示例下载,改变以满足您的需求。注意我通过按钮单击事件执行此操作,您无需执行此操作。
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