一個簡單的下載程式VB.NET樣本

來源:互聯網
上載者:User

一個簡單的下載程式

Code:
  1. Public Shared fpath As String  
  2.     Dim fsize As Long  
  3.   
  4.   
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
  6.         Try  
  7.             If Me.TextBox1.Text = "http://" Then  
  8.                 MessageBox.Show("請輸入正確的URL!")   
  9.                 Return  
  10.             End If  
  11.             Dim urld As String  
  12.             Dim i As Integer  
  13.             Dim dfile As New WebClient   
  14.             Me.SaveFileDialog1.ShowDialog()   
  15.             fpath = SaveFileDialog1.FileName   
  16.   
  17.             Label3.Text = "檔案儲存在:" & fpath   
  18.             urld = Me.TextBox1.Text   
  19.             Me.Timer1.Enabled = True  
  20.             dfile.DownloadFile(urld, fpath.Substring(Me.TextBox1.Text.ToString.LastIndexOf("."), 4))   
  21.             Me.Timer1.Enabled = False  
  22.         Catch ex As Exception   
  23.             MessageBox.Show(ex.Message)   
  24.             Return  
  25.             Application.Exit()   
  26.         End Try  
  27.           
  28.     End Sub  
  29.   
  30.   
  31.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick   
  32.         Dim th2 As New th1   
  33.   
  34.         Dim tt As New System.Threading.Thread(AddressOf th2.timeth) '執行個體化線程類    
  35.   
  36.         tt.Start() '啟動線程    
  37.   
  38.         Label2.Text = th2.downk '顯示下載檔案的大小    
  39.   
  40.         '進行計算下載速度    
  41.   
  42.         Static k As Integer  
  43.   
  44.         Dim p, downspeed   
  45.   
  46.         p = k   
  47.   
  48.         k = p + 1   
  49.   
  50.         p = k   
  51.   
  52.         Dim form As New DownLoad   
  53.   
  54.         Dim fileinfo As New System.IO.FileInfo(form.fpath)   
  55.   
  56.         downspeed = fileinfo.Length()   
  57.   
  58.         downspeed = downspeed / p   
  59.   
  60.         Label4.Text = "當前下載速度是" & downspeed & "k/m"  
  61.   
  62.     End Sub  
  63. End Class  
  64.   
  65. Public Class th1   
  66.   
  67.     Public Shared downk As String  
  68.   
  69.     Dim form As New DownLoad   
  70.   
  71.     Dim fileinfo As New System.IO.FileInfo(form.fpath)   
  72.   
  73.     Dim fsize As Long  
  74.   
  75.     Public Sub timeth()   
  76.   
  77.         If fileinfo.Exists = True Then  
  78.   
  79.             fsize = fileinfo.Length()   
  80.   
  81.             downk = "以下載檔案" & fsize & "k"  
  82.   
  83.         End If  
  84.   
  85.     End Sub  
  86.   
  87. End Class  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.