Gibt die Größe einer Datei in Bytes, KB, MB oder GB als String zurück.
Public Shared Function GetFileSize(ByVal path As String) As String
Dim myFile As FileInfo
Dim mySize As Single
Try
myFile = New FileInfo(path)
If Not myFile.Exists Then
mySize = 0
Else
mySize = myFile.Length
End If
Select Case mySize
Case 0 To 1023
Return mySize & " Bytes"
Case 1024 To 1048575
Return Format(mySize / 1024, "###0.00") & " KB"
Case 1048576 To 1043741824
Return Format(mySize / 1024 ^ 2, "###0.00") & " MB"
Case Is > 1043741824
Return Format(mySize / 1024 ^ 3, "###0.00") & " GB"
End Select
Return "0 bytes"
Catch ex As Exception
Return "0 bytes"
End Try
End Function
Kommentare zum Snippet