Jak rekurzivně zjistit velikost složky
Přidáno: 7.1.2008
Kategorie: VB.NET - Algoritmy
Autor: Ondřej Linhart
Následující metodu můžete použít pro zjištění velikosti složky. Funkce rekurzivně prochází všechny podřízené složky a sčítá velikosti nalezených souborů.
Pro vyšší efektivitu a možnost přerušení operace doporučuji spouštět metodu v samostatném vlákně což zde nehodlám ukazovat.
Public Function GetDirectoryLength(ByVal path As String) As Long
If Not IO.Directory.Exists(path) Then Return 0
Dim length As Long, _
parentDirectory As New DirectoryInfo(path)
Try
For Each file As FileInfo In parentDirectory.GetFiles()
length += file.Length
Next
For Each directory As DirectoryInfo In parentDirectory.GetDirectories()
length += GetDirectoryLength(directory.FullName)
Next
Catch ex As FileNotFoundException
Catch ex As UnauthorizedAccessException
End Try
Return length
End Function