title: What’s the best way to calculate the size of a directory in VB .NET?
body: <p>I need to calculate the directory size in VB .Net</p>
  
  <p>I know the following 2 methods</p>
  
  <p>Method 1: from MSDN <a href="http://msdn.microsoft.com/en-us/library/system.io.directory.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.io.directory.aspx</a></p>
  
  <p>' The following example calculates the size of a directory
  ' and its subdirectories, if any, and displays the total size
  ' in bytes.</p>
  
  <pre><code>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 &lt;&gt; 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
  </code></pre>
  
  <p>Method 2: from <a href="https://stackoverflow.com/questions/468119/whats-the-best-way-to-calculate-the-size-of-a-directory-in-net">What&#39;s the best way to calculate the size of a directory in .NET?</a></p>
  
  <pre><code>Dim size As Int64 = (From strFile In My.Computer.FileSystem.GetFiles(strFolder, _
                FileIO.SearchOption.SearchAllSubDirectories) _
                Select New System.IO.FileInfo(strFile).Length).Sum()
  </code></pre>
  
  <p>Both these methods work fine. However they take lot of time to calculate the directory size if there are lot of sub-folders. e.g i have a directory with 150,000 sub-folders. The above methods took around 1 hr 30 mins to calculate the size of the directory. However, if I check the size from windows it takes less than a minute.</p>
  
  <p>Please suggest better and faster ways to calculate the size of the directory.</p>