26 | 02 | 2017

Get file size including

 Code below displays a size of a specific file. The GetFileSize function will return the size including the appropriate unit formatted by the FormatSize function. 

Imports System.IO

Private Function GetFileSize(ByVal FilePath As String) As Long

'***************************************
'***** Code from visiblevisual.com *****
'***************************************

Dim chkFile As New FileInfo(FilePath)
Dim FileSize As Long = chkFile.Length
Return FormatSize(FileSize)
End Function


Function FormatSize(ByVal fileSize As Double) As String

   If fileSize < 1024 Then
       Return String.Format("{0:N0} B", fileSize)
   ElseIf (fileSize < 1024 * 1024) Then
       Return String.Format("{0:N2} KB", fileSize / 1024)
   ElseIf (fileSize < 1024 * 1024 * 1024) Then
       Return String.Format("{0:N2} MB", fileSize / (1024 ^ 2))
   ElseIf (fileSize < 1099511627776) Then
       Return String.Format("{0:N2} GB", fileSize / (1024 ^ 3))
   Else
       Return String.Format("{0:N2} TB", fileSize / (1024 ^ 4))
   End If
   2 
End Function
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2179718
Latest Articles