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
'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"
oBuffer = New System.IO.StreamWriter(sOPFile)
oBuffer.WriteLine(Now())
Get_Directory_Size("c:\")
oBuffer.Close()
End Sub
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
'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"
oBuffer = New System.IO.StreamWriter(sOPFile)
oBuffer.WriteLine(Now())
Get_Directory_Size("c:\")
oBuffer.Close()
End Sub
No comments:
Post a Comment