• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1474
  • Last Modified:

Find Folder Size in VB.Net

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

1 Solution
I hope this helps you out: http://www.freevbcode.com/ShowCode.asp?ID=4287.

Kind regards,

Matthias Vance
Luis PérezSoftware Architect in .NetCommented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now