Library code snippets
How Big is that File – in English?
Humans and computers sometimes just don’t get along. Take file sizes, for example. What a human being would call one gigabyte, a computer would call 1073741824 bytes. How do you translate one into the other? Pull up a chair.
The following handy function takes a number of bytes and translates it into a readable “human” string. Here’s the code:
Public Function ConvertBytes(ByVal Bytes As Long) As String
' Converts bytes into a readable "1.44 MB", etc. string
If Bytes >= 1073741824 Then
Return Format(Bytes / 1024 / 1024 / 1024, "#0.00") _
& " GB"
ElseIf Bytes >= 1048576 Then
Return Format(Bytes / 1024 / 1024, "#0.00") & " MB"
ElseIf Bytes >= 1024 Then
Return Format(Bytes / 1024, "#0.00") & " KB"
ElseIf Bytes > 0 And Bytes < 1024 Then
Return Fix(Bytes) & " Bytes"
Else
Return "0 Bytes"
End If
End Function
Here’s an example of the function in use. Here, the length of my file is 3027676 bytes—and the ConvertBytes function returns “2.89MB”. Perfect:
Dim objInfo As New System.IO.FileInfo("c:\myfile.bmp")
MessageBox.Show("File is " & ConvertBytes(objInfo.Length))
Related articles
Related discussion
-
Why choose c# when there is Java
by Thaj06 (0 replies)
-
Change color while typing in rich text box
by Akhtar Hussain (0 replies)
-
Third party components to enhance User Interface of Windows based application
by Shaila14041981 (0 replies)
-
VB.NET DataGridView Sort
by bradhen (0 replies)
-
About Comparison in Asp.Net
by S_Rahul (0 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
does really working ..lemme try it ...
This thread is for discussions of How Big is that File – in English?.