代码如下: 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