對于VB.NET語言的應(yīng)用來說,其中有很多小技巧值得我們?nèi)フ莆?,來充?shí)我們的開發(fā)經(jīng)驗(yàn),以提高編程效率。比如VB.NET下載程序的實(shí)現(xiàn),就是其中一個(gè)很有用的編程技巧,能幫助我們很好的解決開發(fā)中所遇到的相關(guān)問題。
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
- 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)
- '實(shí)例化線程類
- tt.Start() '啟動(dòng)線程
- Label2.Text = th2.downk '顯示下載文件的大小
- '進(jìn)行計(jì)算下載速度
- 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 = "當(dāng)前下載速度是" & 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
VB.NET下載程序的相關(guān)代碼操作就為大家介紹到這里。