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? 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.
I know how to convert KB to MB - the problem is how do I incorporate that into my code? Do I add another variable
(Sorry for the previous answer with 1024, a mistaken assumption)
To your question of converting from kB to MB, you can surely assume by SI standard:
1 MB = 1000 kB
Ergo, divide by 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:
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)
divide by 1000?
re: HOW I INCORPORATE THAT INTO MY CODE - DO I ADD ANOTHER VARIABLE
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).
I would just say strMax = objFileInfo.FullName & ' ' & (dblSize / 1024) & 'MB'
(sorry about the syntax, I haven't done VB in > 10 years)
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
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.
Since people like the adv way, this is the easy/simple way ;)
精彩评论