如何从字节使用公共函数返回KB,MB和GB [英] How to return KB, MB and GB from Bytes using a public function

查看:302
本文介绍了如何从字节使用公共函数返回KB,MB和GB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个功能,返回文件的大小(以B,KB,MB,GB)。

在VB.Net code总是得到字节大小第一,所以当一个文件的大小(以字节为单位),小于100则返回B,如果> 1000,然后我把它1000并返回KB 。但是,当它应该是MB我尝试除以1000000返回总规模出来比它要大2 MB的!?

有人能告诉我什么,我做错了!

示例

我的文件大小(15570550字节)..这是......(14.8 MB)

所以,当我通过这个功能运行它返回16MB!

的code

 公共功能GetFileSize(BYVAL TheFile作为字符串_
                            可选BYVAL ShowSizeType由于布尔=假)作为字符串
    如果TheFile.Length = 0则返回
    如果不System.IO.File.Exists(TheFile)则返回
    ---
    昏暗TheSize作为整数= My.Computer.FileSystem.GetFileInfo(TheFile).Length
    昏暗SizeType作为字符串=
    ---
    如果TheSize< 1000然后
        SizeType =B的
    其他
        如果TheSize< 10亿再
            如果TheSize<百万然后
                SizeType =KB
                TheSize = TheSize / 1000
            其他
                SizeType =MB
                TheSize = TheSize /百万
            结束如果
        其他
            SizeType =国标

        结束如果
    结束如果
    ---
    如果ShowSizeType = TRUE,则
        返回TheSize和放大器; SizeType
    其他
        返回TheSize
    结束如果
端功能
 

解决方案

我会用一个选择的情况下,并没有如果。
并总是以最大的规模。我停在结核病但科西嘉岛,你可以添加,如果你需要更多的......

我改昏暗TheSize作为整数到暗淡TheSize作为乌龙,否则大的数字不工作。

另外,还要暗淡的昏暗DoubleBytes作为双,你会用它在选择的情况下。照片

首先你比较,你有一个情况下,字节,可以说MB案件1048576到1073741823
因此,如果是这种情况转换TheSize到一个双DoubleBytes = CDbl(TheSize / 1048576)'MB的

然后在返回您使用FormatNumber设置要显示后面的数字。 在nuber 2是将其设置为2落后等等28.11,将其更改为0,它会返回28。也因为你知道它MB您西港岛线添加和放大器; MB到回报。
返回FormatNumber(DoubleBytes,2)及MB

 私人小组的button1_Click(发送者为对象,E作为EventArgs的)把手Button1.Click
    MSGBOX(GetFileSize(E:\ SOFTWARE \ TeamSpeak3-客户win64-3.0.14.exe))
结束小组

昏暗DoubleBytes作为双

公共职能GetFileSize(BYVAL TheFile作为字符串)作为字符串
    如果TheFile.Length = 0则返回
    如果不System.IO.File.Exists(TheFile)则返回
    ---
    昏暗TheSize作为ULONG = My.Computer.FileSystem.GetFileInfo(TheFile).Length
    昏暗SizeType作为字符串=
    ---

    尝试
        选择案例TheSize
            案例> = 1099511627776
                DoubleBytes = CDbl(TheSize / 1099511627776)的结核病
                返回FormatNumber(DoubleBytes,2)及TB
            案例1073741824到1099511627775
                DoubleBytes = CDbl(TheSize / 1073741824)GB
                返回FormatNumber(DoubleBytes,2)及国标
            案例1048576到1073741823
                DoubleBytes = CDbl(TheSize / 1048576)MB
                返回FormatNumber(DoubleBytes,2)及MB
            案例1024 1048575
                DoubleBytes = CDbl(TheSize / 1024)'的KB
                返回FormatNumber(DoubleBytes,2)及KB
            案例0〜1023
                DoubleBytes = TheSize'字节
                返回FormatNumber(DoubleBytes,2)及字节
            案例否则
                返回 
        最终选择
    抓住
        返回 
    结束尝试
端功能
 


我做了一个DLL它。
然后,我可以把它导入到我的项目,我可以调用它,每当我需要改变一个字节数为其他像MB等
FormatBytes(GetHDSizeF)GetHDSizeF是字节数

 暗淡DoubleBytes作为双
默认的公共属性FormatBytes(BYVAL BytesCaller作为ULONG)作为字符串
    得到
        尝试
            选择案例BytesCaller
                案例> = 1099511627776
                    DoubleBytes = CDbl(BytesCaller / 1099511627776)的结核病
                    返回FormatNumber(DoubleBytes,2)及TB
                案例1073741824到1099511627775
                    DoubleBytes = CDbl(BytesCaller / 1073741824)GB
                    返回FormatNumber(DoubleBytes,2)及国标
                案例1048576到1073741823
                    DoubleBytes = CDbl(BytesCaller / 1048576)MB
                    返回FormatNumber(DoubleBytes,2)及MB
                案例1024 1048575
                    DoubleBytes = CDbl(BytesCaller / 1024)'的KB
                    返回FormatNumber(DoubleBytes,2)及KB
                案例0〜1023
                    DoubleBytes = BytesCaller'字节
                    返回FormatNumber(DoubleBytes,2)及字节
                案例否则
                    返回 
            最终选择
        抓住
            返回 
        结束尝试
    最终获取
    设置(值作为字符串)

    结束设定
高端物业
 


如果你不想让一个dll,你可以使用它像这样的正常功能。

 私人小组的button1_Click(发送者为对象,E作为EventArgs的)把手Button1.Click
    MSGBOX(FormatBytes(2000))
结束小组

昏暗DoubleBytes作为双
公共职能FormatBytes(BYVAL BytesCaller作为ULONG)作为字符串

    尝试
        选择案例BytesCaller
            案例> = 1099511627776
                DoubleBytes = CDbl(BytesCaller / 1099511627776)的结核病
                返回FormatNumber(DoubleBytes,2)及TB
            案例1073741824到1099511627775
                DoubleBytes = CDbl(BytesCaller / 1073741824)GB
                返回FormatNumber(DoubleBytes,2)及国标
            案例1048576到1073741823
                DoubleBytes = CDbl(BytesCaller / 1048576)MB
                返回FormatNumber(DoubleBytes,2)及MB
            案例1024 1048575
                DoubleBytes = CDbl(BytesCaller / 1024)'的KB
                返回FormatNumber(DoubleBytes,2)及KB
            案例0〜1023
                DoubleBytes = BytesCaller'字节
                返回FormatNumber(DoubleBytes,2)及字节
            案例否则
                返回 
        最终选择
    抓住
        返回 
    结束尝试

端功能
 

I'm writing a "function" that returns a file's size (in B, KB, MB, GB).

The VB.Net code always gets the size in bytes first, so when a file's size (in Bytes) is less than 100 it returns B, if its > 1000 then I divide it by 1000 and it returns KB. But when it should be MB I try dividing by 1000000 and the size it returns always comes out 2 MBs bigger than what it should be!?

Can someone please tell me what I'm doing wrong!!

Example

My files size is (15,570,550 Bytes) ..which is.. (14.8 MB)

So when i run it through this function it returns 16MB!

The Code

Public Function GetFileSize(ByVal TheFile As String, _
                            Optional ByVal ShowSizeType As Boolean = False) As String
    If TheFile.Length = 0 Then Return ""
    If Not System.IO.File.Exists(TheFile) Then Return ""
    '---
    Dim TheSize As Integer = My.Computer.FileSystem.GetFileInfo(TheFile).Length
    Dim SizeType As String = ""
    '---
    If TheSize < 1000 Then
        SizeType = "B"
    Else
        If TheSize < 1000000000 Then
            If TheSize < 1000000 Then
                SizeType = "KB"
                TheSize = TheSize / 1000
            Else
                SizeType = "MB"
                TheSize = TheSize / 1000000
            End If
        Else
            SizeType = "GB"

        End If
    End If
    '---
    If ShowSizeType = True Then
        Return TheSize & SizeType
    Else
        Return TheSize
    End If
End Function

解决方案

I would use a select case for it and not a if.
And always start with the biggest size." i stopped at TB but of Corse you can add more if you need ..."

I changed Dim TheSize As Integer to "Dim TheSize As ULong " otherwise big numbers don't work.

Also make a dim "Dim DoubleBytes As Double" you will use it in the select case.

First you compare the bytes you have with a case , lets say mb "Case 1048576 To 1073741823"
So if this is the case convert TheSize to a double "DoubleBytes = CDbl(TheSize / 1048576) 'MB "

Then in the return you use FormatNumber to set the number you want to show behind the . "the nuber 2 is to set it to 2 behind the . like 28.11 , change it to 0 and it will return 28" also because you know it mb you wil add & mb to the return.
"Return FormatNumber(DoubleBytes, 2) & " MB" "

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox(GetFileSize("E:\Software\TeamSpeak3-Client-win64-3.0.14.exe"))
End Sub

Dim DoubleBytes As Double

Public Function GetFileSize(ByVal TheFile As String) As String
    If TheFile.Length = 0 Then Return ""
    If Not System.IO.File.Exists(TheFile) Then Return ""
    '---
    Dim TheSize As ULong = My.Computer.FileSystem.GetFileInfo(TheFile).Length
    Dim SizeType As String = ""
    '---

    Try
        Select Case TheSize
            Case Is >= 1099511627776
                DoubleBytes = CDbl(TheSize / 1099511627776) 'TB
                Return FormatNumber(DoubleBytes, 2) & " TB"
            Case 1073741824 To 1099511627775
                DoubleBytes = CDbl(TheSize / 1073741824) 'GB
                Return FormatNumber(DoubleBytes, 2) & " GB"
            Case 1048576 To 1073741823
                DoubleBytes = CDbl(TheSize / 1048576) 'MB
                Return FormatNumber(DoubleBytes, 2) & " MB"
            Case 1024 To 1048575
                DoubleBytes = CDbl(TheSize / 1024) 'KB
                Return FormatNumber(DoubleBytes, 2) & " KB"
            Case 0 To 1023
                DoubleBytes = TheSize ' bytes
                Return FormatNumber(DoubleBytes, 2) & " bytes"
            Case Else
                Return ""
        End Select
    Catch
        Return ""
    End Try
End Function


I made a dll for it.
Then I can import it to my project and I can call it whenever I need to change a byte number to something else "like mb etc"
FormatBytes(GetHDSizeF) "GetHDSizeF is the number of bytes"

Dim DoubleBytes As Double
Default Public Property FormatBytes(ByVal BytesCaller As ULong) As String
    Get
        Try
            Select Case BytesCaller
                Case Is >= 1099511627776
                    DoubleBytes = CDbl(BytesCaller / 1099511627776) 'TB
                    Return FormatNumber(DoubleBytes, 2) & " TB"
                Case 1073741824 To 1099511627775
                    DoubleBytes = CDbl(BytesCaller / 1073741824) 'GB
                    Return FormatNumber(DoubleBytes, 2) & " GB"
                Case 1048576 To 1073741823
                    DoubleBytes = CDbl(BytesCaller / 1048576) 'MB
                    Return FormatNumber(DoubleBytes, 2) & " MB"
                Case 1024 To 1048575
                    DoubleBytes = CDbl(BytesCaller / 1024) 'KB
                    Return FormatNumber(DoubleBytes, 2) & " KB"
                Case 0 To 1023
                    DoubleBytes = BytesCaller ' bytes
                    Return FormatNumber(DoubleBytes, 2) & " bytes"
                Case Else
                    Return ""
            End Select
        Catch
            Return ""
        End Try
    End Get
    Set(value As String)

    End Set
End Property


And if you don't want to make a dll you can use it like a normal function like this.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox(FormatBytes(2000))
End Sub

Dim DoubleBytes As Double
Public Function FormatBytes(ByVal BytesCaller As ULong) As String

    Try
        Select Case BytesCaller
            Case Is >= 1099511627776
                DoubleBytes = CDbl(BytesCaller / 1099511627776) 'TB
                Return FormatNumber(DoubleBytes, 2) & " TB"
            Case 1073741824 To 1099511627775
                DoubleBytes = CDbl(BytesCaller / 1073741824) 'GB
                Return FormatNumber(DoubleBytes, 2) & " GB"
            Case 1048576 To 1073741823
                DoubleBytes = CDbl(BytesCaller / 1048576) 'MB
                Return FormatNumber(DoubleBytes, 2) & " MB"
            Case 1024 To 1048575
                DoubleBytes = CDbl(BytesCaller / 1024) 'KB
                Return FormatNumber(DoubleBytes, 2) & " KB"
            Case 0 To 1023
                DoubleBytes = BytesCaller ' bytes
                Return FormatNumber(DoubleBytes, 2) & " bytes"
            Case Else
                Return ""
        End Select
    Catch
        Return ""
    End Try

End Function

这篇关于如何从字节使用公共函数返回KB,MB和GB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆