Solved

Find Folder Size in VB.Net

Posted on 2008-10-08
2
1,458 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studios 1 90
MailAddress in vb 4 36
make control visible based off other control value 7 24
vb.net deleting excel sheet in workbook 11 36
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

697 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