VB.NET 下载文件及显示进程

简单的下载

1.创建 WebClient 类,再输入要下载的资源 url 地址还有本地保存路径即可。

1
2
Dim DownloadClient As New Net.WebClient() 
DownloadClient.DownloadFile(yoururladdress,yourfileaddress)

2.使用 DownloadFile 的方法下载文件,指定文件的下载地址和本地路径,和超时时间。

1
My.Computer.Network.DownloadFile(yoururladdress, yourfileaddress, False, 500)

具有进度提示的下载

以 VS2015 为编译环境,举例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Imports System.Net
Public Class Main

Dim DownloadClient As New WebClient

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler DownloadClient.DownloadProgressChanged, AddressOf ShowDownProgress
AddHandler DownloadClient.DownloadDataCompleted, AddressOf DownloadDataCompleted
End Sub

Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button.Click
DownLoadFiles()
End Sub

Private Sub DownLoadFiles()
DownloadClient.DownloadFileAsync(New Uri(yourlink), (yoursaveaddress))
End Sub

Private Sub ShowDownProgress(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Invoke(New Action(Of Integer)(Sub(i) ProgressBar.Value = i), e.ProgressPercentage)
End Sub

Sub DownloadDataCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
MessageBox.Show("下载成功!", "提示", MessageBoxButtons.OK)
End Sub
End Class
Mastodon