Convert KB to MB?

SEE LOWER THIS MAIL FOR UPDATE ON THIS PLEASE.

I have the code below that does a directory search and displays the largest file in a directory. the problem is that it displays it in KB - how can I convert it to MB? The file size comes out too much, so you need to make reading easier - 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 

MUST MAKE IT YOURSELF CLEAN UP! I KNOW HOW TO MAKE KB TO MB, BUT THERE IS NO IDEA AS I INSERT THAT IN MY CODE - I ADD ANOTHER VARIABLE FOR STRMAX AS / 1024.EXCEPT I ALREADY HAVE A STRMAX CHANGE ..... EVERYTHING VERY VERY TIME.

I know how to convert KB to MB - the problem is how can I include this in my code? Am I adding another variable

+4
source share
5 answers

(Sorry for the previous answer with 1024, erroneous assumption)

To your question about converting from kB to MB, you can certainly assume according to the SI standard:

 1 MB = 1000 kB 

Ergo, divide by 1000.

For the unconvinced, I recommend that you read this .

Since software such as Microsoft Windows expresses the storage amount 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 print dblMax and your file size is in bytes, not kB)

+5
source

divide by 1000?

re: HOW I INCLUDE THAT IN MY CODE - I ADD ANOTHER VARIABLE

you can add another variable if you want, it will be easier to debug. Just give him a new name. You can also perform inline division (see @KevinDTimm Solution).

+3
source

I would just say strMax = objFileInfo.FullName & ' ' & (dblSize / 1024) & 'MB'

(sorry for the syntax, I did not do VB after> 10 years)

0
source
 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 
0
source

Put it 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 renaming some things according to 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 to MB, which is mainly used for downloading.

Since people like promotion, this is a simple / easy way;)

0
source

All Articles