OSVBNET 1,386 Reputation points

Getting file size in bytes is as easy as:

(New System.IO.FileInfo(blahfile).Length)

But why can't find an easy short solution to get folder size in
What's the best approach?

  • is this fine?
         Dim PathInfo As DirectoryInfo = New DirectoryInfo(MyDialog.SelectedPath)  
         Dim FileInfo() As FileInfo = PathInfo.GetFiles("*.*", SearchOption.AllDirectories)  
         Dim TotalFolderSize As Long = 0  
         For MyLoop As Integer = 0 To FileInfo.Length - 1  
             TotalFolderSize += New System.IO.FileInfo(FileInfo(MyLoop).FullName).Length  

if so: GetFiles(" * . * " ...

. will include multi extension files? or if use a single asterisk like " * " will also include all files?

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,637 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points

    I played around with this code a fair while ago. Very fast but, not good where there are special system file types/folder types included.

    Public Class Form1  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Dim fso = CreateObject("Scripting.FileSystemObject")  
        Dim pr = fso.GetFolder("C:\Users\lesha\AppData\Local\Temp")  
        Dim sz As Double = pr.Size / 1024 / 1024  
        MessageBox.Show(sz.ToString("0.00 MB"))  
      End Sub  
    End Class  
    0 comments No comments

  2. Castorix31 82,656 Reputation points

    You can see this thread where the OP tested (nearly) all methods : Fastest way to get size of a directory including all files and sub folders
    (reading MFT is the fastest method)

    0 comments No comments