Windows Phone Developers

Friday, June 15, 2007

Directory Size using VB.Net

Get Folder Size using .Net

For getting the size of each directory in Vb.Net, you need to add up the size of each file in the directory.

This function gets the size of each directory including sub-directories and writes to a text file

Imports System.IO

Dim oBuffer As System.IO.TextWriter

Function Get_Directory_Size(ByVal sDirPath As String)

Dim lDirSize As Long
Dim oDir As DirectoryInfo
Dim sDir As DirectoryInfo
Dim sFiles As FileInfo
Dim iFN As Short '* File Number
Dim sPath As String '* Saving Path

'Dim oFSW As System.IO.FileSystemWatcher

oDir = New DirectoryInfo(sDirPath)

Dim oDirs As DirectoryInfo() = oDir.GetDirectories()

For Each sDir In oDirs

lDirSize = 0

For Each sFiles In sDir.GetFiles("*.*")
lDirSize += sFiles.Length

' Coded by Shasur for

'MsgBox("Size of Directory " & sDir.Name & " is " & lDirSize)
oBuffer.WriteLine(sDir.FullName & vbTab & lDirSize)

Get_Directory_Size(sDirPath & sDir.Name & "\")


End Function

The procedure below uses WriteLine function of the StreamWriter class

Sub Store_Size_Of_Directory

Dim sOPFile As String

sOPFile = c:\SystemDirSize.log"

' Coded by Shasur for

oBuffer = New System.IO.StreamWriter(sOPFile)


End Sub Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon


  1. This code only count size of subdirectories of the path not include file in fix path

  2. "not include file in fix path " - do you mean to list the files