How to return KB, MB and GB from Bytes using a public function

前端 未结 6 1552
囚心锁ツ
囚心锁ツ 2021-02-10 01:49

I\'m writing a "function" that returns a file\'s size (in B, KB, MB, GB).

The VB.Net code always gets the size in bytes first, so when a file\'s size (in Bytes)

6条回答
  •  囚心锁ツ
    2021-02-10 02:05

      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
    
        Public Sub GetfilesizeFromDirectory()
            Dim path As String
            path = "D:"
            TotalSize = 0 'Reset the counter
            Dim TheSize As Long = GetDirSize(path)
            ' TextBox1.Text = (FormatNumber(TheSize / 1024 / 1024 / 1) & vbCr & "MB")
        End Sub
    

提交回复
热议问题