?
Solved

Find Folder Size in VB.Net

Posted on 2008-10-08
2
Medium Priority
?
1,460 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

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