FolderSize

OSVBNET 1,386 Reputation points
2022-09-01T20:19:57.17+00:00

Hello,
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 vb.net?
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  
         Next  
    

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

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

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

2 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-09-01T21:13:19.553+00:00

    Hi
    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 81,461 Reputation points
    2022-09-01T21:30:48.56+00:00

    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