代碼如下: Imports System.Net'到入命名空間 Public Class Form1 Inherits System.Windows.Forms.Form Public Shared fpath As String Dim fsize As Long PRivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim urld As String Dim i As Integer Dim dfile As New WebClient()'實例化WebClient類 SaveFileDialog1.ShowDialog() fpath = SaveFileDialog1.FileName Label3.Text = "文件保存在" & fpath urld = Label1.Text + TextBox1.Text Timer1.Enabled = True dfile.DownloadFile(urld, fpath)'下載指定nrl地址的文件 Timer1.Enabled = False End Sub Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Me.Refresh()'用來刷新窗口 Dim th2 As New th1() Dim tt As New System.Threading.Thread(AddressOf th2.timeth) 實例化線程類 tt.Start()'啟動線程 Label2.Text = th2.downk'顯示下載文件的大小 '進行計算下載速度 Static k As Integer Dim p,downspeed p = k k = p + 1 p = k Dim fileinfo As New System.IO.FileInfo(form.fpath) downspeed = fileinfo.Length() downspeed= downspeed/p label4.textbox= "當前下載速度是"& downspeed & "k/m" End Sub End Class Public Class th1 Public Shared downk As String Dim form As New Form1() Dim fileinfo As New System.IO.FileInfo(form.fpath) Dim fsize As Long Public Sub timeth() If fileinfo.Exists = True Then fsize = fileinfo.Length() downk = "以下載文件" & fsize & "k" End If End Sub End Class
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click Dim th As WebRequest = WebRequest.Create("http://download2.ssreader.com/download/SSR362b5B(0802).exe") '這個站是超星的網站,SSR362b5B(0802).exe文件是超星圖書閱讀器 Dim w As WebResponse = th.GetResponse() Dim j As Long j = w.ContentLength().ToString'獲得文件大小 Debug.WriteLine(j) End Sub