Something like the following. Note that if it's a number it should be stored in a datatype designed for numbers. The only time a number should be stored in a string is for display purposes (eg comma separation, units, etc)
Dim FInfo As New System.IO.FileInfo(Server.MapPath(strVirtualPath))
Dim Result As String
Try
If oInfo.Exists Then
Dim FileSize = FInfo.Length
If FileSize < 1048576 Then
Result = System.Math.Round(FileSize / 1024, 2) & " kb"
Else
Result = System.Math.Round(FileSize / 1048576, 2) & " mb"
End If
End If
Catch ex As Exception
....
Note that this is still inadequate (unless you know you'll never get a file smaller than 1KB or larger than 1GB). A much better solution can be found here
Edit: In response to André Figueiredo... FileInfo.Length is defined as:
Public ReadOnly Property Length() As Long
<SecuritySafeCritical()>
Get
If Me._dataInitialised = -1 Then
MyBase.Refresh()
End If
If Me._dataInitialised <> 0 Then
__Error.WinIOError(Me._dataInitialised, MyBase.DisplayPath)
End If
If(Me._data.fileAttributes And 16) <> 0 Then
__Error.WinIOError(2, MyBase.DisplayPath)
End If
Return CLng(Me._data.fileSizeHigh) << 32 Or (CLng(Me._data.fileSizeLow) And CLng((CULng(-1))))
End Get
End Property
The _dataInitialised
seems to be set by the (native) base class so I can't see when it's set. One would hope it's on construction but that doesn't seem to be the case as it's checked in a number of places.
Of course, it's all moot since the OP seems to be using Option Strict = False
which adds ~30% performance penalty due to all the type checking.