Solved

Find Folder Size in VB.Net

Posted on 2008-10-08
2
1,459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
MatthiasVance earned 500 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

728 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