Find Folder Size in VB.Net

Posted on 2008-10-08
Last Modified: 2012-06-22
I have some code where I am getting information about folders.
The one I can't figure out is how to get the size of the directory. How would I do this?
Dim getInfo As System.IO.DirectoryInfo

getInfo = My.Computer.FileSystem.GetDirectoryInfo(stPath)


With rowFolder

.FullPath = getInfo.FullName

.CreatedTime = getInfo.CreationTime

.LastWritten = getInfo.LastWriteTime

.NumFiles = getInfo.GetFiles.Length

.NumSubDirs = getInfo.GetDirectories.Length

End With

Open in new window

Question by:jdhackett

Accepted Solution

MatthiasVance earned 500 total points
ID: 22670126
I hope this helps you out:

Kind regards,

Matthias Vance
LVL 25

Expert Comment

by:Luis Pérez
ID: 22673479
You can do it easily with this recursivity function. Check it, you'll the that you get the same result size that if you test the folder with Windows Explorer.
Private Function GetFolderSize(ByVal Folder As DirectoryInfo) As Integer

    Dim iSize As Integer = 0

    'The folder size of "this" folder is the sum of all files length

    'plus the sum of each subfolder size

    'Get the size of all files

    Dim aFiles() As FileInfo = Folder.GetFiles()

    For Each oFile As FileInfo In aFiles

        iSize += oFile.Length


    'Iterate through subfolders to get their size, too

    Dim aFolders() As DirectoryInfo = Folder.GetDirectories()

    For Each oFolder As DirectoryInfo In aFolders

        iSize += GetFolderSize(oFolder)


    'Return total sum

    Return iSize

End Function

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now