Calculate Folder Size in Dot Net

Thinking of it ? Why Dot Net or My don't provide th ease of knowing the folder size ?
k Bro you have to do it ur self, but if you didn't this is the code 4 u


Public abort As Boolean
Function GetFolderSize(ByVal DirPath As String, ByVal includeSubFolders As Boolean) As Long
Try
Dim size As Long = 0
Dim diBase As New DirectoryInfo(DirPath)
Dim files() As FileInfo
If includeSubFolders Then
files = diBase.GetFiles("*", SearchOption.AllDirectories)

Else
files = diBase.GetFiles("*", SearchOption.TopDirectoryOnly)
End If
Dim ie As IEnumerator = files.GetEnumerator
While ie.MoveNext And Not abort
size += DirectCast(ie.Current, FileInfo).Length
End While
Return size
Catch ex As Exception
MsgBox("Error: " & ex.Message)
Return -1
End Try
End Function

0 comments:

Post a Comment