폴더 파일및 사이즈

VNote 2017.07.07 15:55:28 *: Public Class Form1
Dim TotalSize As Long = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TotalSize = 0 'Reset the counter
Dim TheSize As Long = GetDirSize("D:\SW_ST")
MsgBox(FormatNumber(TheSize, 0) & " Bytes" & vbCr & _
FormatNumber(TheSize / 1024, 1) & " Kilobytes" & vbCr & _
FormatNumber(TheSize / 1024 / 1024, 1) & " Megabytes" & vbCr & _
FormatNumber(TheSize / 1024 / 1024 / 1024, 1) & " Gigabytes")
End Sub
Public Function GetDirSize(RootFolder As String) As Long
Dim FolderInfo = New IO.DirectoryInfo(RootFolder)
For Each File In FolderInfo.GetFiles : TotalSize += File.Length
Next
For Each SubFolderInfo In FolderInfo.GetDirectories : GetDirSize(SubFolderInfo.FullName)
Next
Return TotalSize
End Function
End Class

Public Class Form1
    Dim TotalSize As Long = 0
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TotalSize = 0 'Reset the counter
        Dim TheSize As Long = GetDirSize("e:")
        MsgBox(FormatNumber(TheSize, 0) & " Bytes" & vbCr & _
               FormatNumber(TheSize / 1024, 1) & " Kilobytes" & vbCr & _
               FormatNumber(TheSize / 1024 / 1024, 1) & " Megabytes" & vbCr & _
               FormatNumber(TheSize / 1024 / 1024 / 1024, 1) & " Gigabytes")
    End Sub
    Public Function GetDirSize(RootFolder As String) As Long
        Dim FolderInfo = New IO.DirectoryInfo(RootFolder)

        Try
            For Each File In FolderInfo.GetFiles : TotalSize += File.Length
            Next
            For Each SubFolderInfo In FolderInfo.GetDirectories : GetDirSize(SubFolderInfo.FullName)
            Next
        Catch ex As Exception

        End Try
        Return TotalSize
    End Function
End Class