Private Function GetDirectorySize(ByVal sPath As String, Optional ByVal Units As UnitType = UnitType.Auto) As String
Dim Dir As New DirectoryInfo(sPath)
Dim Bytes As Long
For Each directory As DirectoryInfo In Dir.GetDirectories("*.*", SearchOption.AllDirectories)
Next
For Each File As FileInfo In Dir.GetFiles("*.*", SearchOption.AllDirectories)
Bytes += File.Length
' aantalfiles = aantalfiles + 1
Next
Return FormatSize(Bytes, Units)
End Function
Private Shared Function FormatSize(ByVal Bytes As Long, Optional ByVal Units As UnitType = UnitType.Auto) As String
Select Case Units
Case 0
Return Fix(Bytes) & " Bytes"
Case 1
Return Format(Bytes / 1024, "#0.00") & " KB"
Case 2
Return Format(Bytes / 1024 / 1024, "#0.00") & " MB"
Case 3
Return Format(Bytes / 1024 / 1024 / 1024, "#0.00") & " GB"
Case Else
If Bytes >= 1073741824 Then
Return Format(Bytes / 1024 / 1024 / 1024, "#0.00") & " GB"
ElseIf Bytes >= 1048576 Then
Return Format(Bytes / 1024 / 1024, "#0.00") & " MB"
ElseIf Bytes >= 1024 Then
Return Format(Bytes / 1024, "#0.00") & " KB"
ElseIf Bytes > 0 And Bytes < 1024 Then
Return Fix(Bytes) & " Bytes"
Else
Return "0 Bytes"
End If
End Select
End Function
''' <summary>Data units to get.</summary>
Private Enum UnitType
''' <summary>Returns the size in bytes.</summary>
Bytes
''' <summary>Returns the size in kilobytes.</summary>
Kilobytes
''' <summary>Returns the size in megabytes.</summary>
Megabytes
''' <summary>Returns the size in giabytes.</summary>
Gigabytes
''' <summary>Returns the size based on the file that is being passed in.</summary>
Auto
End Enum
''' <summary>Returns the filesize of a specified file.</summary>
''' <param name="sFile">The file to get the filesize of.</param>
''' <param name="Units">The data unit to use (GB, MB, KB or Bytes).</param>
''' <returns>Returns a string containing the filesize.</returns>