gpt4 book ai didi

vb.net - 如何使用公共(public)函数从 Bytes 返回 KB、MB 和 GB

转载 作者:行者123 更新时间:2023-12-03 15:06:58 26 4
gpt4 key购买 nike

我正在编写一个返回文件大小(以 B、KB、MB、GB 为单位)的“函数”。
VB.Net 代码总是首先获取以字节为单位的大小,因此当文件的大小(以字节为单位)小于 100 时,它返回 B,如果大于 1000,则我将其除以 1000,然后返回 KB。但是当它应该是 MB 时,我尝试除以 1000000,它返回的大小总是比它应该的大小大 2 MB!?
有人可以告诉我我做错了什么!!
例子
我的文件大小是(15,570,550 字节)..这是..(14.8 MB)
所以当我通过这个函数运行它时,它返回 16MB!
编码

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

最佳答案

我会为它使用一个选择案例,而不是一个 if。
并且总是从最大的尺寸开始。“我在 TB 停了下来,但 Corse 如果你需要,你可以添加更多......”

我将 Dim TheSize As Integer 更改为“Dim TheSize As ULong”,否则大数字不起作用。

还要制作一个暗淡的“Dim DoubleBytes As Double”,您将在选择案例中使用它。

首先,您将拥有的字节与 case 进行比较,比如说 mb "Case 1048576 To 1073741823"
因此,如果是这种情况,请将 TheSize 转换为双倍 "DoubleBytes = CDbl(TheSize/1048576) 'MB
"

然后在返回中使用 FormatNumber 设置要在 . “nuber 2 将其设置为 2 后面。就像 28.11 一样,将其更改为 0 它将返回 28”也因为您知道它 mb 您将在返回值中添加 & mb。
"返回 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

我为它制作了一个dll。
然后我可以将它导入到我的项目中,我可以在需要将字节数更改为其他“如 mb 等”时调用它
FormatBytes(GetHDSizeF) "GetHDSizeF 是字节数"
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

如果你不想制作一个 dll,你可以像这样的普通函数一样使用它。
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

关于vb.net - 如何使用公共(public)函数从 Bytes 返回 KB、MB 和 GB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27367190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com