Morpheusweb.it - Risorse per webmaster: script, ASP.NET, C#, Visual Basic .Net, tutorial, asp, javascript, css, php, html, java, ADO, VBScript, forms, frames, Active Server Pages, Dynamic HTML, database, gratis per webmaster e webdesigner

Folder size - << C# VERSION >>


Calculate the size of a Directory


<%@ Page language="VB"%>
<%@ Import Namespace="System.IO" %>
<HTML>
<script language="VB" runat="server">

Sub Page_Load()
  Dim strFolderPath As String = "./Upload"
  Dim clsDirectorySize As New DirectorySize
  lbDimensioneCartella.Text = "Folder size: " & _
  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

End Class

</script>
<body>
<asp:Label ID="lbDimensioneCartella" runat="server"></asp:Label>
</body>
</HTML>



Download Code...


Print Page


 








Page top

risorse per webmaster