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
Next

'--------------------------------------------------------
' Coded by Shasur for http://dotnetdud.blogspot.com/
'--------------------------------------------------------


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

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

Next


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 http://dotnetdud.blogspot.com/
'--------------------------------------------------------

oBuffer = New System.IO.StreamWriter(sOPFile)
oBuffer.WriteLine(Now())

Get_Directory_Size("c:\")

oBuffer.Close()
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

2 comments:

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

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

    ReplyDelete