Groote berkenen

Status
Niet open voor verdere reacties.

w00tare

Gebruiker
Lid geworden
10 jul 2009
Berichten
216
Oke, hij geeft een error aan, iemand?

Code:
    Public Enum convTo 'Enumerations for file size conversions
        B = 0 ' Byte
        KB = 1 ' Kilobyte
        MB = 2 ' Megabyte
        GB = 3 ' Gigabyte
        TB = 4 ' Terabyte
        PB = 5 ' Petabyte
        EB = 6 ' Exabyte
        ZI = 7 ' Zettabyte
        YI = 8 ' Yottabyte
    End Enum

    Public Function ConvertBytes(ByVal bytes As Long, ByVal convertTo As convTo) As Double ' Credits (IDK)

        If convTo.IsDefined(GetType(convTo), convertTo) Then

            Return bytes / (1024 ^ convertTo)

        Else

            Return -1 'An invalid value was passed to this function so exit

        End If

    End Function

    Public Function ConvertByte(ByVal fileSize As Long) As String ' Credits Pl0xy

        Dim sizeOfKB As Long = 1024              ' Actual size in bytes of 1KB
        Dim sizeOfMB As Long = 1048576           ' 1MB
        Dim sizeOfGB As Long = 1073741824        ' 1GB
        Dim sizeOfTB As Long = 1099511627776     ' 1TB
        Dim sizeofPB As Long = 1125899906842624  ' 1PB
        Dim sizeofEB As Long = 1152921504606846976 '1EB
        ' Dim sizeofZB As Long = 1180591620717411303424 '1ZB
        ' Dim sizeofYB as Long = 1208925819614629174706176 '1YT
        ' ZB & YB zijn te lang

        Dim tempFileSize As Double
        Dim tempFileSizeString As String

        Dim myArr() As Char = {CChar("0"), CChar(".")}  'Characters to strip off the end of our string after formating

        If fileSize < sizeOfKB Then ' Filesize is in Bytes
            tempFileSize = ConvertBytes(fileSize, convTo.B)
            If tempFileSize = -1 Then Return Nothing 'Invalid conversion attempted so exit
            tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr) ' Strip the 0's and 1's off the end of the string
            Return Math.Round(tempFileSize) & " bytes (" & tempFileSizeString & " bytes)" 'Return our converted value

        ElseIf fileSize >= sizeOfKB And fileSize < sizeOfMB Then ' Filesize is in Kilobytes
            tempFileSize = ConvertBytes(fileSize, convTo.KB)
            If tempFileSize = -1 Then Return Nothing
            tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
            Return Math.Round(tempFileSize) & " KB (" & tempFileSizeString & " bytes)"

        ElseIf fileSize >= sizeOfMB And fileSize < sizeOfGB Then '  Filesize is in Megabytes
            tempFileSize = ConvertBytes(fileSize, convTo.MB)
            If tempFileSize = -1 Then Return Nothing
            tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
            Return Math.Round(tempFileSize, 1) & " MB (" & tempFileSizeString & " bytes)"

        ElseIf fileSize >= sizeOfGB And fileSize < sizeOfTB Then ' Filesize is in Gigabytes
            tempFileSize = ConvertBytes(fileSize, convTo.GB)
            If tempFileSize = -1 Then Return Nothing
            tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
            Return Math.Round(tempFileSize, 1) & " GB (" & tempFileSizeString & " bytes)"

        ElseIf fileSize >= sizeOfTB And fileSize < sizeofPB Then ' Filesize is in Terabytes
            tempFileSize = ConvertBytes(fileSize, convTo.TB)
            If tempFileSize = -1 Then Return Nothing
            tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
            Return Math.Round(tempFileSize, 1) & " TB (" & tempFileSizeString & " bytes)"

        ElseIf fileSize >= sizeofPB And fileSize < sizeofPB Then ' Filesize is in Petabytes || Even erbij gedaan voor het geval als terabyte overflowed
            tempFileSize = ConvertBytes(fileSize, convTo.PB)
            If tempFileSize = -1 Then Return Nothing
            tempFileSizeString = Format(fileSize, "Standard").TrimEnd(myArr)
            Return Math.Round(tempFileSize, 1) & " PB (" & tempFileSizeString & " bytes)"
            ' Groter kan niet lijkt mij
        Else
            Return Nothing ' Foute filesize dus niks
        End If
    End Function
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan