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
Comment Utility
I hope this helps you out:

Kind regards,

Matthias Vance
LVL 25

Expert Comment

by:Luis Pérez
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

10 Experts available now in Live!

Get 1:1 Help Now