65.9K
CodeProject is changing. Read more.
Home

Print File Size

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Jul 19, 2010

CPOL
viewsIcon

11234

You can keep your output consistent with how Windows represents file sizes by using the inbuilt function;StrFormatByteSizeThe following code will dump the files from the root of C:\, Call getFilesAndSizes from a button click or wherever you want. ...

You can keep your output consistent with how Windows represents file sizes by using the inbuilt function;StrFormatByteSize The following code will dump the files from the root of C:\, Call getFilesAndSizes from a button click or wherever you want.
    <System.Runtime.InteropServices.DllImport("shlwapi", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    Private Shared Function StrFormatByteSize( _
         ByVal fileSize As Long, _
         ByVal buffer As System.Text.StringBuilder, _
         ByVal bufferSize As Integer
        ) As Long
    End Function

    Private Sub GetFilesAndSizes()
        dirFiles("C:\")
    End Sub

    Private Sub dirFiles(ByVal path As String)

        Dim fi As System.IO.FileInfo
        Dim sb As New System.Text.StringBuilder
        Dim files() As String
        Try
            files = System.IO.Directory.GetFiles(path)

            For Each file In files
                fi = New System.IO.FileInfo(file)
                StrFormatByteSize(fi.Length, sb, 128)

                Debug.WriteLine(file & " : " & sb.ToString)
            Next

        Catch ex As Exception
            Debug.WriteLine("Error with: " & path)
        End Try

    End Sub
The output generated on my laptop was;
C:\autoexec.bat : 24 bytes
C:\bootmgr : 374 KB
C:\BOOTSECT.BAK : 8.00 KB
C:\config.sys : 10 bytes
C:\hiberfil.sys : 2.24 GB
C:\pagefile.sys : 2.99 GB