-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsUpdateDownloader.vb
118 lines (105 loc) · 3.9 KB
/
clsUpdateDownloader.vb
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
Imports System.Net
Imports System.IO
Public Class WebFileDownloader
Public Event AmountDownloadedChanged(ByVal iNewProgress As Long)
Public Event FileDownloadSizeObtained(ByVal iFileSize As Long)
Public Event FileDownloadComplete()
Public Event FileDownloadFailed(ByVal ex As Exception)
Private mCurrentFile As String = String.Empty
Public Enum chkModus
fehler = 0
update = 1
ok = 3
End Enum
Public chkUpdateModus As chkModus
Public ReadOnly Property CurrentFile() As String
Get
Return mCurrentFile
End Get
End Property
Public Function DownloadFile(ByVal URL As String, ByVal Location As String) As Boolean
Try
mCurrentFile = GetFileName(URL)
Dim WC As New WebClient
WC.DownloadFile(URL, Location)
RaiseEvent FileDownloadComplete()
Return True
Catch ex As Exception
RaiseEvent FileDownloadFailed(ex)
Return False
End Try
End Function
Private Function GetFileName(ByVal URL As String) As String
Try
Return URL.Substring(URL.LastIndexOf("/") + 1)
Catch ex As Exception
Return URL
End Try
End Function
Public Function DownloadFileWithProgress(ByVal URL As String, ByVal Location As String, Optional ByVal bWithProgress As Boolean = True) As Boolean
Dim FS As FileStream = Nothing
Try
mCurrentFile = GetFileName(URL)
Dim wRemote As WebRequest
Dim bBuffer As Byte()
ReDim bBuffer(256)
Dim iBytesRead As Integer
Dim iTotalBytesRead As Integer
FS = New FileStream(Location, FileMode.Create, FileAccess.Write)
wRemote = WebRequest.Create(URL)
Dim myWebResponse As WebResponse = wRemote.GetResponse
If bWithProgress = True Then
RaiseEvent FileDownloadSizeObtained(myWebResponse.ContentLength)
End If
Dim sChunks As Stream = myWebResponse.GetResponseStream
Do
Application.DoEvents()
iBytesRead = sChunks.Read(bBuffer, 0, 256)
FS.Write(bBuffer, 0, iBytesRead)
iTotalBytesRead += iBytesRead
If myWebResponse.ContentLength < iTotalBytesRead Then
If bWithProgress = True Then
RaiseEvent AmountDownloadedChanged(myWebResponse.ContentLength)
End If
Else
If bWithProgress = True Then
RaiseEvent AmountDownloadedChanged(iTotalBytesRead)
End If
End If
Loop While Not iBytesRead = 0
sChunks.Close()
FS.Close()
RaiseEvent FileDownloadComplete()
Return True
Catch ex As Exception
If Not (FS Is Nothing) Then
FS.Close()
FS = Nothing
End If
RaiseEvent FileDownloadFailed(ex)
Return False
End Try
End Function
Public Shared Function FormatFileSize(ByVal Size As Long) As String
Try
Dim KB As Integer = 1024
Dim MB As Integer = KB * KB
' Return size of file in kilobytes.
If Size < KB Then
Return (Size.ToString("D") & " bytes")
Else
Select Case Size / KB
Case Is < 1000
Return (Size / KB).ToString("N") & "KB"
Case Is < 1000000
Return (Size / MB).ToString("N") & "MB"
Case Is < 10000000
Return (Size / MB / KB).ToString("N") & "GB"
End Select
End If
Return Size.ToString
Catch ex As Exception
Return Size.ToString
End Try
End Function
End Class