如何根据asp.net中的字节,kb,mb和gb来区分文件大小? [英] How to differentiate file size based on bytes,kb,mb and gb in asp.net ?
问题描述
Hi
我的文件大小为表格行中的字节数(row1(collength)。ToString),如果每个语句我需要显示文件大小,包括字节或kb或mb或gb。
我试试这样,但是当手动检查文件中的大小时它不匹配。
Hi
I have a file size as bytes in table row(row1("collength").ToString) ,using if each statement i need to show file size with bytes or kb or mb or gb.
I try like this ,but when manually check size in file it not match.
Dim docsize As Integer = row1("collength").ToString
If docsize = 1024 Then
MessageBox("1" + " bytes,")
ElseIf docsize <= 1024 Then
MessageBox(docsize.ToString + " bytes,")
ElseIf docsize > 1024 And docsize <= 9999 Then
Dim fsize As Integer = docsize / 1024
tree_flow.fsize = fsize.ToString + " bytes,"
MessageBox( fsize.ToString + " bytes,")
ElseIf docsize > 10000 And docsize <= 999999 Then
Dim fsize As Integer = docsize / 1024
MessageBox(fsize.ToString + " KB,")
Else
Dim fsize As Integer = docsize / 1048576
MessageBox(fsize.ToString + " MB,")
End If
注意:在第1行(col长度)。ToString我有以下值的字节数,但值是一次一个
9350195-- 8.91 MB
1394 - -1.36 KB
132488757 - 126 MB
24 --- 24字节
276992 --- 270 KB
98304 - 90 KB
如果是9350195,那么我需要显示8.91 mb或8 mb或9 mb的消息,bcz当我使用除以1024将字节转换为kb时,我得到9。
如果条件也加入GB。
请尽快回复我
问候
Aravind
Note : in row1("collength").ToString i have following value in bytes,but values are one at a time
9350195-- 8.91 MB
1394 --1.36 KB
132488757 -- 126 MB
24 --- 24 bytes
276992 --- 270 KB
98304 -- 90 KB
In above if 9350195 then i need to show message like 8.91 mb or 8 mb or 9 mb,bcz when i convert bytes as kb using divide by 1024 i get 9 .
Pls add GB in if condition also.
Pls reply me asap
Regards
Aravind
推荐答案
您可以使用一个方法接受字节数的值并根据值返回一个字符串:
You could use a method accepting the value in number of bytes and returning a string according to the value:
Public Shared Function GetValueWithAdequateUnit(value As ULong) As String
Dim unitValue As Double
Dim unit As String
If (value >= 1024 * 1024 * 1024 * 1024)
unitValue = CType(value, Double) / (1024 * 1024 * 1024 * 1024)
unit = "TB"
Else If (value >= 1024 * 1024 * 1024)
unitValue = CType(value, Double) / (1024 * 1024 * 1024)
unit = "GB"
Else If (value >= 1024 * 1024)
unitValue = CType(value, Double) / (1024 * 1024)
unit = "MB"
Else If (value >= 1024)
unitValue = CType(value, Double) / 1024
unit = "KB"
Else
unitValue = value
unit = "B"
End If
Return String.Format("{0} {1}", unitValue, unit)
End Function
我用了一个文本框和按钮
受保护的子按钮1_Click(发送者作为对象,e作为事件Args)处理Button1.Click
Dim docsize As Double = TextBox1.Text
如果docsize = 1024那么
ShowMessage(" 1" +字节,")
ElseIf docsize< = 1024然后
ShowMessage(docsize.ToString +" bytes,")
ElseIf docsize> 1024和docsize< = 9999然后
Dim fsize As Double = docsize / 1024
Dim roun As Double = Decimal.Round(fsize ,2,MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString +" bytes,")
ElseIf docsize> 10000和docsize< = 999999然后
Dim fsize As Integer = docsize / 1024
Dim roun As Double = Decimal.Round(fsize,2,MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString +" KB,")
ElseIf docsize> 1000000和docsize< = 99999999然后
Dim fsize As Double = docsize / 1048576
Dim roun As Double = Decimal.Round(fsize ,2,MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString +" MB,")
Else
Dim fsize As Double = docsize / 1073741824
Dim roun As Double = Decimal.Round(fsize,2,MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString +" GB,")
结束如果
End Sub
I used one text box & button
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim docsize As Double = TextBox1.Text
If docsize = 1024 Then
ShowMessage("1" + " bytes,")
ElseIf docsize <= 1024 Then
ShowMessage(docsize.ToString + " bytes,")
ElseIf docsize > 1024 And docsize <= 9999 Then
Dim fsize As Double = docsize / 1024
Dim roun As Double = Decimal.Round(fsize, 2, MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString + " bytes,")
ElseIf docsize > 10000 And docsize <= 999999 Then
Dim fsize As Integer = docsize / 1024
Dim roun As Double = Decimal.Round(fsize, 2, MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString + " KB,")
ElseIf docsize > 1000000 And docsize <= 99999999 Then
Dim fsize As Double = docsize / 1048576
Dim roun As Double = Decimal.Round(fsize, 2, MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString + " MB,")
Else
Dim fsize As Double = docsize / 1073741824
Dim roun As Double = Decimal.Round(fsize, 2, MidpointRounding.AwayFromZero)
ShowMessage(roun.ToString + " GB,")
End If
End Sub
这篇关于如何根据asp.net中的字节,kb,mb和gb来区分文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!