Easy FileSize method





6
Date Submitted Thu. Sep. 21st, 2006 7:07 AM
Revision 1 of 1
Scripter Pio
Tags filesize | System.IO | VB.NET
Comments 1 comments
An all in one function that formats a given file's Size from bytes to gB.

  Public Shared Function FileSize(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
 

Allen Gammel

p-soft.org
Allen / Pio
email: intolerance@gmail.com

Comments

Comments Thanks
Fri. Jul. 6th, 2007 7:28 AM    Beginner babakzawari

Voting