[vb.net] Ottenere la dimensione di una directory.
Imports System Imports System.IO Public Class Form1 Private Sub Bt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt1.Click Dim DirectoryOrigine As String = "c:\windows" Try 'Chiamando la function DimCartella con True come secondo parametro 'effettua il conteggio del totale dimensione files 'incluse le eventuali sottocartelle 'Se il parametro e' False considera solo i files presenti nella radice Dim s As Long = DimCartella(DirectoryOrigine, True) MessageBox.Show(s.ToString("#0,0") & " bytes") Catch ex As Exception MessageBox.Show(ex.Message & Environment.NewLine & ex.StackTrace) End Try End Sub Function DimCartella(ByVal sPath As String, ByVal bRecursive As Boolean) As Long Dim Size As Long = 0 Dim lngNumberOfDirectories As Long = 0 Dim diDir As New DirectoryInfo(sPath) Try Dim fil As FileInfo For Each fil In diDir.GetFiles() Size += fil.Length Next fil If bRecursive = True Then Dim diSubDir As DirectoryInfo For Each diSubDir In diDir.GetDirectories() Size += DimCartella(diSubDir.FullName, True) lngNumberOfDirectories += 1 Next diSubDir End If Return Size Catch ex As Exception MessageBox.Show(ex.Message & Environment.NewLine & ex.StackTrace) End Try End Function End Class