?
Solved

Find Folder Size in VB.Net

Posted on 2008-10-08
2
Medium Priority
?
1,467 Views
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?
Thanks
Dim getInfo As System.IO.DirectoryInfo
 
getInfo = My.Computer.FileSystem.GetDirectoryInfo(stPath)
<snip>
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

0
Comment
Question by:jdhackett
2 Comments
 
LVL 8

Accepted Solution

by:
MatthiasVance earned 2000 total points
ID: 22670126
I hope this helps you out: http://www.freevbcode.com/ShowCode.asp?ID=4287.

Kind regards,

Matthias Vance
0
 
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
    Next
 
    'Iterate through subfolders to get their size, too
    Dim aFolders() As DirectoryInfo = Folder.GetDirectories()
    For Each oFolder As DirectoryInfo In aFolders
        iSize += GetFolderSize(oFolder)
    Next
 
    'Return total sum
    Return iSize
End Function

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 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