[英]Convert KB to MB?
SEE BOTTOM OF THIS POST FOR UPDATE ON THIS PLEASE. 请查看此帖的底部,以便更新。
I have the below code that searches through directories and displays the largest file in the directory. 我有以下代码搜索目录并显示目录中的最大文件。 the problem is that it displays it in KB - how on earth do I convert it to MB? 问题是它以KB格式显示 - 我究竟如何将其转换为MB? The file size comes out way too large so want easier reading - thanks for the help: 文件大小太大,所以想要更容易阅读 - 感谢您的帮助:
Private Sub btnGetMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetMax.Click
ClearList()
Dim dblSize As Integer = 0
Dim dblMax As Integer = 0
Dim strMax As String = ""
Dim objFileInfo As System.IO.FileInfo
For Each strFile As String In My.Computer.FileSystem.GetFiles("c:\temp", FileIO.SearchOption.SearchAllSubDirectories)
objFileInfo = My.Computer.FileSystem.GetFileInfo(strFile)
/*whats the size of the files?*/
dblSize = objFileInfo.Length
If dblSize > dblMax Then
dblMax = dblSize
strMax = objFileInfo.FullName
End If
Next
MessageBox.Show("Largest file in .Net folder is " & vbCrLf &
strMax & vbCrLf &
dblMax.ToString("N0"))
End Sub
SHOULD HAVE MADE MYSELF MORE CLEAR! 应该让我自己更清楚! I KNOW HOW TO CONVERT KB TO MB BUT NO IDEA HOW I INCORPORATE THAT INTO MY CODE - DO I ADD ANOTHER VARIABLE FOR STRMAX AS /1024.....EXCEPT I ALREADY HAVE STRMAX VARIABLE.....STILL VERY MUCH A BEGINNER GUYS. 我知道如何将KB转换为MB但不知道我如何将其纳入我的代码 - 我是否为STRMAX添加了另一个变量/1024..EXCEPT我已经有了STRMAX VARIABLE ......仍然是非常多的初学者GUYS。
I know how to convert KB to MB - the problem is how do I incorporate that into my code? 我知道如何将KB转换为MB - 问题是如何将其合并到我的代码中? Do I add another variable 我是否添加了另一个变量
(Sorry for the previous answer with 1024, a mistaken assumption) (抱歉,上一个回答是1024,错误的假设)
To your question of converting from kB to MB, you can surely assume by SI standard: 关于从kB转换为MB的问题,您可以通过SI标准来确定:
1 MB = 1000 kB
Ergo, divide by 1000. 因此,除以1000。
For the unconvinced, I encourage you to read this . 对于不相信的,我鼓励你阅读这篇文章 。
Since software like Microsoft Windows expresses storage quantities in multiples of 1024 bytes, change your code to: 由于Microsoft Windows等软件以1024字节的倍数表示存储量,因此请将代码更改为:
dblMax = dblMax/(1024*1024)
MessageBox.Show("Largest file in .Net folder is " & vbCrLf &
strMax & vbCrLf &
dblMax.ToString("N0"))
(since you are printing dblMax & your file size is in bytes, not kB) (因为您正在打印dblMax并且您的文件大小以字节为单位,而不是KB)
divide by 1000? 除以1000?
re: HOW I INCORPORATE THAT INTO MY CODE - DO I ADD ANOTHER VARIABLE re: 我如何将其纳入我的代码 - 我是否添加另一个变量
you can add another variable if you want, it will be easier to do debugging. 你可以根据需要添加另一个变量,这样可以更容易地进行调试。 Just give it a new name. 只要给它一个新名字。 You can also do the division inline (see @KevinDTimm 's solution). 你也可以进行内联划分(参见@KevinDTimm的解决方案)。
Put this at the top of the document. 把它放在文档的顶部。
Dim imin As Integer 'Bytes
Dim imax As Integer 'Bytes
Dim imin1 As Integer 'Kb
Dim imax1 As Integer 'kb
Then try to rename some stuff to match yours. 然后尝试重命名一些东西以匹配你的东西。
Private Sub WC_DownloadProgressChanged(sender As Object, e As System.Net.DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
Try
imin = e.BytesReceived / 1024 'Bytes converted to KB
imax = e.TotalBytesToReceive / 1024 'Bytes converted to KB
imin1 = imin / 1024 'Converts to MB
imax1 = imax / 2014 'Converts to MB
Catch ex As Exception
End Try
Try
ProgressBar1.Maximum = e.TotalBytesToReceive
ProgressBar1.Value = e.BytesReceived
Label1.Text = imin1 & "MB of " & imax1 & "MB"
Catch ex As Exception
End Try
End Sub
This will convert it into MB which is mostly used for downloads. 这会将其转换为MB,主要用于下载。
Since people like the adv way, this is the easy/simple way ;) 由于人们喜欢adv方式,这是简单/简单的方式;)
I would just say strMax = objFileInfo.FullName & ' ' & (dblSize / 1024) & 'MB'
我只想说strMax = objFileInfo.FullName & ' ' & (dblSize / 1024) & 'MB'
(sorry about the syntax, I haven't done VB in > 10 years) (对不起语法,我还没有在> 10年内完成VB)
Enum xByte As Long
kilo = 1024L
mega = 1024L * kilo
giga = 1024L * mega
tera = 1024L * giga
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
For x As Integer = 2 To 4
Debug.WriteLine("")
Dim d As Double = 1024 ^ x
Debug.WriteLine(String.Format("{0} bytes ", d.ToString("n0")))
Debug.WriteLine(String.Format("{0} KB ", (d / xByte.kilo).ToString("n3")))
Debug.WriteLine(String.Format("{0} MB ", (d / xByte.mega).ToString("n3")))
Debug.WriteLine(String.Format("{0} GB ", (d / xByte.giga).ToString("n3")))
Debug.WriteLine(String.Format("{0} TB ", (d / xByte.tera).ToString("n3")))
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.