Sub Page_Load()
Dim strFolderPath As String = "./Upload"
Dim clsDirectorySize As New DirectorySize
lbDimensioneCartella.Text = "Dimensione cartella: " & _
clsDirectorySize.Size(strFolderPath)
End Sub
Public Class DirectorySize
Inherits System.Web.UI.Page
Public Function Size(ByVal virtualPath As String) As String
If virtualPath = "" Then
virtualPath = "/"
ElseIf virtualPath.EndsWith("/") Then
virtualPath = virtualPath.Substring(0, virtualPath.Length - 1)
End If
Dim physicalPath As String = Server.MapPath(virtualPath)
Dim folderSize As Double = GetFolderSize(physicalPath)
Dim strSize As String = FormatSize(folderSize)
Return strSize
End Function
Private Function GetFolderSize(ByVal physicalPath As String) As Double
Dim dblDirSize As Double = 0
Dim objDirInfo As DirectoryInfo = New DirectoryInfo(physicalPath)
Dim arrChildFiles As Array = objDirInfo.GetFiles()
Dim arrSubFolders As Array = objDirInfo.GetDirectories()
Dim objChildFile As FileInfo
Dim objChildFolder As DirectoryInfo
For Each objChildFile In arrChildFiles
dblDirSize += objChildFile.Length
Next
For Each objChildFolder In arrSubFolders
dblDirSize += GetFolderSize(objChildFolder.FullName)
Next
Return dblDirSize
End Function
Private Function FormatSize(ByVal dblFileSize As Double) As String
If dblFileSize < 1024 Then
Return String.Format("{0:N0} B", dblFileSize)
ElseIf dblFileSize < 1024 * 1024 Then
Return String.Format("{0:N2} KB", dblFileSize / 1024)
ElseIf dblFileSize < 1024 * 1024 * 1024 Then
Return String.Format("{0:N2} MB", dblFileSize / (1024 * 1024))
ElseIf dblFileSize >= 1024 * 1024 * 1024 Then
Return "Dimensione in GB!"
End If
End Function