I need to calculate directory size in VB.Net
I know the following 2 methods
Method 1: from MSDN http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
'The following example calculates the size of a directory' and its subdirectories, if any, and displays the total size 'in bytes.
Imports System
Imports System.IO
Public Class ShowDirSize
Public Shared Function DirSize(ByVal d As DirectoryInfo) As Long
Dim Size As Long = 0
' Add file sizes.
Dim fis As FileInfo() = d.GetFiles()
Dim fi As FileInfo
For Each fi In fis
Size += fi.Length
Next fi
' Add subdirectory sizes.
Dim dis As DirectoryInfo() = d.GetDirectories()
Dim di As DirectoryInfo
For Each di In dis
Size += DirSize(di)
Next di
Return Size
End Function 'DirSize
Public Shared Sub Main(ByVal args() As String)
If args.Length <> 1 Then
Console.WriteLine("You must provide a directory argument at the command line.")
Else
Dim d As New DirectoryInfo(args(0))
Dim dsize As Long = DirSize(d)
Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, dsize)
End If
End Sub 'Main
End Class 'ShowDirSize
Method 2: of What is the best way to calculate the size of a directory in .NET?
Dim size As Int64 = (From strFile In My.Computer.FileSystem.GetFiles(strFolder, _
FileIO.SearchOption.SearchAllSubDirectories) _
Select New System.IO.FileInfo(strFile).Length).Sum()
Both of these methods work fine. However, they take a long time to calculate the size of the directory if there are many subfolders. for example, I have a directory with 150,000 subfolders. The above methods took about 1 hour 30 minutes to calculate the size of the directory. However, if I check the size of the window, it takes less than a minute.
, .