Formatting a Number of Bytes as a String with Unit

StrFormatByteSize can be used to format a number of bytes as a “human-readable” string. For example, 100,000,000 will be formatted as “95,3 MB” on a de-DE system:

Private Declare Auto Function StrFormatByteSize Lib "shlwapi.dll" ( _
    ByVal qdw As UInt64, _
    ByVal szBuf As String, _
    ByVal uiBufSize As Int32 _
) As IntPtr

Private Function FormatBytes(ByVal Bytes As UInt64) As String
    Dim s As String = Space(256)
    If StrFormatByteSize(Bytes, s, s.Length).Equals(IntPtr.Zero) Then
        Throw New Exception("Conversion failed.")
        Return Left(s, InStr(s, ControlChars.NullChar) - 1)
    End If
End Function