1 Public Shared Function FormatFileSize(ByVal byteLenght As Long) As String
2 'smart function
3 Select Case byteLenght
4 Case Is < 1024
5 'bytes, very small
6 Return byteLenght.ToString("0 bytes")
7 Case Is < 1048576
8 'Kb
9 Return (byteLenght / 1024).ToString("0 KB")
10 Case Is < 1073741824
11 'MB
12 Return (byteLenght / 1048576).ToString("0 MB")
13 Case Else
14 'GB
15 Return (byteLenght / 1073741824).ToString("0 GB")
16 End Select
17
18 End Function
19
20'------- OR
21
22 Public Shared Function FormatFileSize(ByVal byteLenght As Long, ByVal formatType As String, ByVal numberOnly As Boolean) As String
23 If formatType = "" Then formatType = "AUTO"
24
25 'smart function
26 If UCase(formatType) = "AUTO" Then
27 Select Case byteLenght
28 Case Is < 1024
29 'bytes, very small
30 If numberOnly Then
31 Return byteLenght.ToString("0")
32 Else
33 Return byteLenght.ToString("0 bytes")
34 End If
35 Case Is < 1048576
36 'Kb
37 If numberOnly Then
38 Return (byteLenght / 1024).ToString("0")
39 Else
40 Return (byteLenght / 1024).ToString("0 KB")
41 End If
42 Case Is < 1073741824
43 'MB
44 If numberOnly Then
45 Return (byteLenght / 1048576).ToString("0")
46 Else
47 Return (byteLenght / 1048576).ToString("0 MB")
48 End If
49 Case Else
50 'GB
51 If numberOnly Then
52 Return (byteLenght / 1073741824).ToString("0")
53 Else
54 Return (byteLenght / 1073741824).ToString("0 GB")
55 End If
56 End Select
57 Else
58 Select Case UCase(formatType)
59 Case "BYTES"
60 'bytes, very small
61 If numberOnly Then
62 Return byteLenght.ToString("0")
63 Else
64 Return byteLenght.ToString("0 bytes")
65 End If
66 Case "KB"
67 'Kb
68 If numberOnly Then
69 Return (byteLenght / 1024).ToString("0")
70 Else
71 Return (byteLenght / 1024).ToString("0 KB")
72 End If
73 Case "MB"
74 'MB
75 If numberOnly Then
76 Return (byteLenght / 1048576).ToString("0")
77 Else
78 Return (byteLenght / 1048576).ToString("0 MB")
79 End If
80 Case "GB"
81 'GB
82 If numberOnly Then
83 Return (byteLenght / 1073741824).ToString("0")
84 Else
85 Return (byteLenght / 1073741824).ToString("0 GB")
86 End If
87 Case Else
88 '? auto
89 Return FormatFileSize(byteLenght, "AUTO", numberOnly)
90 End Select
91 End If
92 End Function