对于VB.NET语言的应用来说,其中有很多小技巧值得我们去掌握,来充实我们的开发经验,以提高编程效率。比如VB.NET下载程序的实现,就是其中一个很有用的编程技巧,能帮助我们很好的解决开发中所遇到的相关问题。
VB.NET下载程序代码示例:
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
Try
If Me.TextBox1.Text = "http://" Then
MessageBox.Show("请输入正确的URL!")
Return
End If
Dim urld As String
Dim i As Integer
Dim dfile As New WebClient
Me.SaveFileDialog1.ShowDialog()
fpath = SaveFileDialog1.FileName
Label3.Text = "文件保存在:" & fpath
urld = Me.TextBox1.Text
Me.Timer1.Enabled = True
dfile.DownloadFile(urld, fpath.Substring
(Me.TextBox1.Text.ToString.LastIndexOf("."), 4))
Me.Timer1.Enabled = False
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
Application.Exit()
End Try
End Sub
- 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.
Private Sub Timer1_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Timer1.Tick
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 form As New DownLoad
Dim fileinfo As New System.IO.FileInfo(form.fpath)
downspeed = fileinfo.Length()
downspeeddownspeed = downspeed / p
Label4.Text = "当前下载速度是" & downspeed & "k/m"
End Sub
End Class
Public Class th1
Public Shared downk As String
Dim form As New DownLoad
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
- 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.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
VB.NET下载程序的相关代码操作就为大家介绍到这里。
【编辑推荐】