Posted By

indra on 05/22/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

D4RKCODE


Filesize Converter


 / Published in: VB.NET
 

This is module for converting filesize, round to highest unit : G, Mb, Kb, B

  1. Module ModFilesizeConversion
  2. Public Function ConvertSize(ByVal fileSize As Long) As String
  3.  
  4. Dim sizeOfKB As Long = 1024 ' Actual size in bytes of 1KB
  5. Dim sizeOfMB As Long = 1048576 ' 1MB
  6. Dim sizeOfGB As Long = 1073741824 ' 1GB
  7. Dim sizeOfTB As Long = 1099511627776 ' 1TB
  8. Dim sizeofPB As Long = 1125899906842624 ' 1PB
  9.  
  10. Dim tempFileSize As Double
  11. Dim tempFileSizeString As String
  12.  
  13. Dim myArr() As Char = {CChar("0"), CChar(".")} 'Characters to strip off the end of our string after formating
  14.  
  15. If fileSize < sizeOfKB Then 'Filesize is in Bytes
  16. tempFileSize = ConvertBytes(fileSize, convTo.B)
  17. If tempFileSize = -1 Then Return Nothing 'Invalid conversion attempted so exit
  18. tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr) ' Strip the 0's and 1's off the end of the string
  19. Return Math.Round(tempFileSize) & " bytes" 'Return our converted value
  20.  
  21. ElseIf fileSize >= sizeOfKB And fileSize < sizeOfMB Then 'Filesize is in Kilobytes
  22. tempFileSize = ConvertBytes(fileSize, convTo.KB)
  23. If tempFileSize = -1 Then Return Nothing 'Invalid conversion attempted so exit
  24. tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
  25. Return Math.Round(tempFileSize) & " KB"
  26.  
  27. ElseIf fileSize >= sizeOfMB And fileSize < sizeOfGB Then ' Filesize is in Megabytes
  28. tempFileSize = ConvertBytes(fileSize, convTo.MB)
  29. If tempFileSize = -1 Then Return Nothing 'Invalid conversion attempted so exit
  30. tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
  31. Return Math.Round(tempFileSize, 1) & " MB"
  32.  
  33. ElseIf fileSize >= sizeOfGB And fileSize < sizeOfTB Then 'Filesize is in Gigabytes
  34. tempFileSize = ConvertBytes(fileSize, convTo.GB)
  35. If tempFileSize = -1 Then Return Nothing
  36. tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
  37. Return Math.Round(tempFileSize, 1) & " GB"
  38.  
  39. ElseIf fileSize >= sizeOfTB And fileSize < sizeofPB Then 'Filesize is in Terabytes
  40. tempFileSize = ConvertBytes(fileSize, convTo.TB)
  41. If tempFileSize = -1 Then Return Nothing
  42. tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
  43. Return Math.Round(tempFileSize, 1) & " TB"
  44.  
  45. 'Anything bigger than that is silly ;)
  46.  
  47. Else
  48.  
  49. Return Nothing 'Invalid filesize so return Nothing
  50.  
  51. End If
  52.  
  53. End Function
  54.  
  55. Public Function ConvertBytes(ByVal bytes As Long, ByVal convertTo As convTo) As Double
  56.  
  57. If convTo.IsDefined(GetType(convTo), convertTo) Then
  58.  
  59. Return bytes / (1024 ^ convertTo)
  60.  
  61. Else
  62.  
  63. Return -1 'An invalid value was passed to this function so exit
  64.  
  65. End If
  66.  
  67. End Function
  68.  
  69. Public Enum convTo
  70. B = 0
  71. KB = 1
  72. MB = 2
  73. GB = 3 'Enumerations for file size conversions
  74. TB = 4
  75. PB = 5
  76. EB = 6
  77. ZI = 7
  78. YI = 8
  79. End Enum
  80. End Module

Report this snippet  

You need to login to post a comment.