[VB2010]HyperURLGetter─專門讀取網路資源的類別

利用HyperURLGetter類別製作簡易的下載器

HyperURLGetter可以應用在許多地方,至於要怎麼使用就要看各位程式設計者是怎麼想的囉!在此提供一個簡易下載器的程式碼,含檢查更新功能。請自行照以下程式碼提供的方式佈置表單控制項元件,然後將程式碼貼入即可。

控制項位置可依各位的喜好隨意擺放。

表單程式碼:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'建立1個Form,並命名為ExampleForm
'建立21個Label,並分別命名為URLLabel、SaveFolderPathLabel、SaveFileNameLabel、SaveFullPathLabel、TimeLabel、TimeLabelExplain、LastTimeLabel、LastTimeLabelExplain、DownloadProgressLabel、SpeedLabelExplain、SpeedLabel、BytesReceivedLabelExplain、BytesReceivedLabel、LastBytesLabelExplain、LastBytesLabel、TotleSizeLabelExplain、TotleSizeLabel、AverageSpeedLabel、AverageSpeedLabelExplain、AverageLastTimeLabel、AverageLastTimeLabelExplain
'建立4個TextBox,並分別命名為URLTextBox、SaveFolderPathTextBox、SaveFileNameTextBox、SaveFullPathTextBox
'建立5個Button,並分別命名為SaveFolderPathButton、SaveFullPathButton、DownloadStartButton、DownloadStopButton、UpdateButton
'建立1個ProgressBar,並命名為DownloadProgressBar
 
Public Class ExampleForm
 
    Dim WithEvents DownloadManager As New HyperURLGetter("http://dlce.antivir.com/package/wks_avira/win32/zhtw/pecl/avira_antivir_personal_zhtw.exe") '建立一個HyperURLGetter實體,用來下載檔案,並且包含事件。
    Dim WithEvents UpdateManager As New HyperURLGetter("http://dl.dropbox.com/u/11347597/update_hyperurlgetter.txt") '建立一個HyperURLGetter實體,用來檢查更新,並且包含事件。
 
    Private Sub ExampleForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load '載入範例表單時
        ArrangeControls() '編排控制項元件(若想自訂的話請自行將這段刪除或是修改)
        FormReFresh() '重新整理表單
    End Sub
 
    Private Sub FileDownloadForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing '關閉表單時
        DownloadManager.StopRunning() '中斷讀取
    End Sub
 
    Private Sub SaveFolderPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFolderPathButton.Click '「瀏覽」「儲存目錄」
        DownloadManager.SaveFolderPath = DownloadManager.ChoseFolderPath '直接開啟對話方塊選取資料夾
        FormReFresh() '重新整理表單
    End Sub
 
    Private Sub SaveFullPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFullPathButton.Click '「瀏覽」「儲存路徑」
        DownloadManager.SaveFullPath = DownloadManager.ChoseFullPath  '直接開啟對話方塊設定路徑及檔名
        FormReFresh() '重新整理表單
    End Sub
 
    Private Sub SaveFileNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFileNameTextBox.TextChanged '當檔名輸入方塊的內容更變時
        DownloadManager.SaveFileName = SaveFileNameTextBox.Text '將更變後的內容存入DownloadManager
        FormReFresh() '重新整理表單
    End Sub
 
    Private Sub SaveFolderPathTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFolderPathTextBox.TextChanged '當儲存目錄輸入方塊的內容更變時
        DownloadManager.SaveFolderPath = SaveFolderPathTextBox.Text '將更變後的內容存入DownloadManager
        FormReFresh() '重新整理表單
    End Sub
 
    Private Sub SaveFullPathTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFullPathTextBox.TextChanged '當儲存路徑輸入方塊的內容更變時
        DownloadManager.SaveFullPath = SaveFullPathTextBox.Text '將更變後的內容存入DownloadManager
        FormReFresh() '重新整理表單
    End Sub
 
    Private Sub URLTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles URLTextBox.TextChanged '當網址輸入方塊的內容更變時
        DownloadManager.TargetURL = URLTextBox.Text '將更變後的內容存入DownloadManager
        FormReFresh() '重新整理表單
    End Sub
 
    Private Sub DownloadStopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadStopButton.Click '停止下載
        DownloadManager.StopRunning() '停止讀取
    End Sub
 
    Private Sub FormReFresh() '重新整理表單
        DownloadManager.CreateSaveFileName() '置入檔案名稱
        URLTextBox.Text = DownloadManager.TargetURL '取得URL
        SaveFolderPathTextBox.Text = DownloadManager.SaveFolderPath '取得儲存目錄
        SaveFileNameTextBox.Text = DownloadManager.SaveFileName '取得儲存檔名
        SaveFullPathTextBox.Text = DownloadManager.SaveFullPath '取得完整儲存路徑
    End Sub
 
    Private Sub DownloadStartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadStartButton.Click '開始下載
        DownloadStartButton.Enabled = False '禁用開始下載按鈕
 
        '初始化DownloadManager
        DownloadManager.TargetURL = URLTextBox.Text
        DownloadManager.SaveFullPath = SaveFullPathTextBox.Text
        DownloadManager.SetHeaders("Chrome") '將DownloadManager模擬為Google瀏覽器
 
        DownloadManager.RunWorkerAsync() '開始讀取
    End Sub
 
    Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click '檢查更新
        UpdateManager.Timeout = 3000 '設定讀取逾時標準(單位:毫秒)
        UpdateManager.RunWorkerAsync() '開始讀取
    End Sub
 
    '-------------------------------------------------DownloadManager事件-------------------------------------------------
 
    Private Sub DownloadManager_Loading() Handles DownloadManager.ProgressLoading 'DownloadManager讀取時
        DownloadProgressBar.Value = DownloadManager.GetProgressPercentage '改變進度列
        TimeLabel.Text = DownloadManager.FormatTime(DownloadManager.GetLoadingTime) '顯示已經過時間
        LastTimeLabel.Text = DownloadManager.FormatTime(DownloadManager.GetLastTime) '顯示剩餘時間
        AverageLastTimeLabel.Text = DownloadManager.FormatTime(DownloadManager.GetAverageLastTime) '顯示平均剩餘時間
        DownloadProgressLabel.Text = DownloadManager.GetDoubleProgressPercentage(2) & "%" '顯示進度百分比
        SpeedLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetEverySecondSpead, 2) & "/s" '顯示速度
        AverageSpeedLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetAverageEverySecondSpead, 2) & "/s" '顯示平均速度
        BytesReceivedLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetBytesReceived, 2) '顯示已接收位元組數
        LastBytesLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetLastBytes, 2) '顯示剩餘接收位元組數
        TotleSizeLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetTotalBytesToReceive, 2) '顯示目標檔案位元組數
    End Sub
 
    Private Sub DownloadManager_Completed() Handles DownloadManager.ProgressCompleted 'DownloadManager讀取完成時
        Select Case DownloadManager.GetStatus '取得狀態傳回值
            Case 0 '讀取失敗
                MsgBox("下載失敗", MsgBoxStyle.Critical)
            Case 1 '讀取成功
                DownloadProgressBar.Value = 100
                DownloadProgressLabel.Text = "100%"
                MsgBox("下載成功。使用時間:" & DownloadManager.FormatTime(DownloadManager.GetLoadedTime), MsgBoxStyle.Information)
            Case 2
                MsgBox("中斷下載", MsgBoxStyle.Exclamation)
        End Select
 
        DownloadStartButton.Enabled = True '啟用開始下載按鈕
    End Sub
 
    '-------------------------------------------------UpdateManager事件-------------------------------------------------
    Private Sub UpdateManager_Completed() Handles UpdateManager.ProgressCompleted 'UpdateManager讀取完成時
        Dim TempText As String = UpdateManager.HyperURLText("HyperURLGetter") '取得讀取到的文字,並且進行特徵判斷
        Select Case UpdateManager.GetStatus '取得狀態傳回值
            Case 0, 3 '讀取失敗,或是特徵不符
                MsgBox("檢查更新失敗!", MsgBoxStyle.Critical)
            Case 1 '讀取成功(檢查更新成功)
                Dim Divide() As String = Strings.Split(TempText, "|") '利用已知特徵分割字串
                If UpdateManager.GetMyVersion = Divide(0) Then '如果目前版本和線上取得的版本資訊相符
                    MsgBox("已經是最新版本!", MsgBoxStyle.Information)
                Else '如果不符
                    MsgBox("已有新版本 " & Divide(0) & vbCrLf & "(目前版本 " & UpdateManager.GetMyVersion & ")", MsgBoxStyle.Information)
                End If
            Case 2 '讀取中斷
                MsgBox("連線過於緩慢,請稍後再檢查更新!", MsgBoxStyle.Exclamation)
        End Select
    End Sub
 
    Private Sub UpdateManager_Timeout() Handles UpdateManager.ProgressTimeout 'UpdateManager讀取逾時
        UpdateManager.StopRunning() '中斷讀取
    End Sub
End Class

排列控制項的程式碼(ArrangeControls):

    Public Sub ArrangeControls() '編排控制項元件(若想自訂的話請自行將這段刪除或是修改)
        'ExampleForm
        ExampleForm.Text = "HyperURLGetter 範例程式(檔案下載器)"
        ExampleForm.Size = New Size(370, 325)
        ExampleForm.Location = New Point((Screen.PrimaryScreen.Bounds.Width / 2) - (ExampleForm.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Height / 2) - (ExampleForm.Size.Height / 2)) '讓ExampleForm位於螢幕中央
        ExampleForm.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
        ExampleForm.MaximizeBox = False
 
        'URLLabel
        ExampleForm.URLLabel.Text = "輸入網址:"
        ExampleForm.URLLabel.Location = New Point(12, 15)
 
        'SaveFolderPathLabel
        ExampleForm.SaveFolderPathLabel.Text = "儲存目錄:"
        ExampleForm.SaveFolderPathLabel.Location = New Point(12, 50)
 
        'SaveFileNameLabel
        ExampleForm.SaveFileNameLabel.Text = "儲存檔名:"
        ExampleForm.SaveFileNameLabel.Location = New Point(12, 84)
 
        'SaveFullPathLabel
        ExampleForm.SaveFullPathLabel.Text = "儲存路徑:"
        ExampleForm.SaveFullPathLabel.Location = New Point(12, 116)
 
        'TimeLabelExplain
        ExampleForm.TimeLabelExplain.Text = "已使用:"
        ExampleForm.TimeLabelExplain.Location = New Point(12, 164)
 
        'TimeLabel
        ExampleForm.TimeLabel.AutoSize = True
        ExampleForm.TimeLabel.Text = "0 秒"
        ExampleForm.TimeLabel.Location = New Point(64, 164)
 
        'LastTimeLabelExplain
        ExampleForm.LastTimeLabelExplain.Text = "預估剩餘時間:"
        ExampleForm.LastTimeLabelExplain.Location = New Point(12, 176)
 
        'LastTimeLabel
        ExampleForm.LastTimeLabel.AutoSize = True
        ExampleForm.LastTimeLabel.Text = "0 秒"
        ExampleForm.LastTimeLabel.Location = New Point(100, 176)
 
        'AverageLastTimeLabelExplain
        ExampleForm.AverageLastTimeLabelExplain.Text = "平均剩餘時間:"
        ExampleForm.AverageLastTimeLabelExplain.Location = New Point(12, 188)
 
        'AverageLastTimeLabel
        ExampleForm.AverageLastTimeLabel.AutoSize = True
        ExampleForm.AverageLastTimeLabel.Text = "0 秒"
        ExampleForm.AverageLastTimeLabel.Location = New Point(100, 188)
 
        'SpeedLabelExplain
        ExampleForm.SpeedLabelExplain.Text = "瞬時速率:"
        ExampleForm.SpeedLabelExplain.Location = New Point(12, 208)
 
        'AverageSpeedLabelExplain
        ExampleForm.AverageSpeedLabelExplain.Text = "平均速率:"
        ExampleForm.AverageSpeedLabelExplain.Location = New Point(199, 208)
 
        'BytesReceivedLabelExplain
        ExampleForm.BytesReceivedLabelExplain.Text = "接收:"
        ExampleForm.BytesReceivedLabelExplain.Location = New Point(223, 228)
 
        'LastBytesLabelExplain
        ExampleForm.LastBytesLabelExplain.Text = "剩餘:"
        ExampleForm.LastBytesLabelExplain.Location = New Point(223, 248)
 
        'TotleSizeLabelExplain
        ExampleForm.TotleSizeLabelExplain.Text = "合計:"
        ExampleForm.TotleSizeLabelExplain.Location = New Point(223, 268)
 
        'DownloadProgressLabel
        ExampleForm.DownloadProgressLabel.AutoSize = False
        ExampleForm.DownloadProgressLabel.Size = New Size(100, 21)
        ExampleForm.DownloadProgressLabel.TextAlign = ContentAlignment.MiddleRight
        ExampleForm.DownloadProgressLabel.Text = "000.00%"
        ExampleForm.DownloadProgressLabel.Location = New Point(245, 164)
 
        'SpeedLabel
        ExampleForm.SpeedLabel.AutoSize = False
        ExampleForm.SpeedLabel.Size = New Size(75, 19)
        ExampleForm.SpeedLabel.TextAlign = ContentAlignment.MiddleRight
        ExampleForm.SpeedLabel.Text = "000.00 KB/s"
        ExampleForm.SpeedLabel.Location = New Point(83, 205)
 
        'AverageSpeedLabel
        ExampleForm.AverageSpeedLabel.AutoSize = False
        ExampleForm.AverageSpeedLabel.Size = New Size(75, 19)
        ExampleForm.AverageSpeedLabel.TextAlign = ContentAlignment.MiddleRight
        ExampleForm.AverageSpeedLabel.Text = "000.00 KB/s"
        ExampleForm.AverageSpeedLabel.Location = New Point(270, 205)
 
        'BytesReceivedLabel
        ExampleForm.BytesReceivedLabel.AutoSize = False
        ExampleForm.BytesReceivedLabel.Size = New Size(75, 19)
        ExampleForm.BytesReceivedLabel.TextAlign = ContentAlignment.MiddleRight
        ExampleForm.BytesReceivedLabel.Text = "000.00 MB"
        ExampleForm.BytesReceivedLabel.Location = New Point(270, 225)
 
        'LastBytesLabel
        ExampleForm.LastBytesLabel.AutoSize = False
        ExampleForm.LastBytesLabel.Size = New Size(75, 19)
        ExampleForm.LastBytesLabel.TextAlign = ContentAlignment.MiddleRight
        ExampleForm.LastBytesLabel.Text = "000.00 MB"
        ExampleForm.LastBytesLabel.Location = New Point(270, 245)
 
        'TotleSizeLabel
        ExampleForm.TotleSizeLabel.AutoSize = False
        ExampleForm.TotleSizeLabel.Size = New Size(75, 19)
        ExampleForm.TotleSizeLabel.TextAlign = ContentAlignment.MiddleRight
        ExampleForm.TotleSizeLabel.Text = "000.00 MB"
        ExampleForm.TotleSizeLabel.Location = New Point(270, 265)
 
        'URLTextBox
        ExampleForm.URLTextBox.Size = New Size(262, 22)
        ExampleForm.URLTextBox.Location = New Point(83, 9)
 
        'SaveFolderPathTextBox
        ExampleForm.SaveFolderPathTextBox.Size = New Size(181, 22)
        ExampleForm.SaveFolderPathTextBox.Location = New Point(83, 43)
 
        'SaveFileNameTextBox
        ExampleForm.SaveFileNameTextBox.Size = New Size(181, 22)
        ExampleForm.SaveFileNameTextBox.Location = New Point(83, 77)
 
        'SaveFullPathTextBox
        ExampleForm.SaveFullPathTextBox.Size = New Size(181, 22)
        ExampleForm.SaveFullPathTextBox.Location = New Point(83, 110)
 
        'SaveFolderPathButton
        ExampleForm.SaveFolderPathButton.Text = "瀏覽"
        ExampleForm.SaveFolderPathButton.Size = New Size(75, 23)
        ExampleForm.SaveFolderPathButton.Location = New Point(270, 42)
 
        'SaveFullPathButton
        ExampleForm.SaveFullPathButton.Text = "瀏覽"
        ExampleForm.SaveFullPathButton.Size = New Size(75, 23)
        ExampleForm.SaveFullPathButton.Location = New Point(270, 109)
 
        'DownloadStartButton
        ExampleForm.DownloadStartButton.Text = "開始下載"
        ExampleForm.DownloadStartButton.Size = New Size(75, 25)
        ExampleForm.DownloadStartButton.Location = New Point(14, 230)
 
        'DownloadStopButton
        ExampleForm.DownloadStopButton.Text = "停止下載"
        ExampleForm.DownloadStopButton.Size = New Size(75, 25)
        ExampleForm.DownloadStopButton.Location = New Point(14, 256)
 
        'UpdateButton
        ExampleForm.UpdateButton.Text = "檢查更新"
        ExampleForm.UpdateButton.Size = New Size(75, 25)
        ExampleForm.UpdateButton.Location = New Point(100, 243)
 
        'DownloadProgressBar
        ExampleForm.DownloadProgressBar.Size = New Size(331, 23)
        ExampleForm.DownloadProgressBar.Location = New Point(14, 138)
    End Sub

執行結果如下:

文章分類:VB.net|標籤:, , , , , ,

迴響已關閉