I'm trying to pull the total physical memory on a machine and have it show just like it would inside My Computer Properties.

I know I can use My.Computer.Info.TotalPhysicalMemory which will return it in bytes. This is where I'm getting stuck. I want to be able to divide it and then truncate it to where it would show like My Computer Properties would report it.

Any suggestions?

after wards you need to divide the number by 1024 to get in Kilo Bytes and again by 1024 to get in MB.
continue the same for GB as well.

Member Avatar for Unhnd_Exception

if your talking about converting it to gigs then divide the bytes by (1024 * 1024 * 1024)

MsgBox(Format((My.Computer.Info.TotalPhysicalMemory / 1024) / 1024, "###,###,##0.00 MB"))
        MsgBox(Format((My.Computer.Info.TotalPhysicalMemory / 1024) / 1024 / 1024, "###,###,##0 GB"))

Hey Codeorder,

I was looking at making a class called Memory to share with everyone. I wanted it to check the size to see if it's just MB of memory or if it's GIG's of memory. I want to make sure that the MB is shown as XXX MB and GB to be shown as X.00 or X.XX is needed. Up to looking at it with me?

Public Class Memory
    Private Function GetMemory()
        Dim MemBitSize As String = My.Computer.Info.TotalPhysicalMemory
        If MemBitSize < "1073741824" And MemBitSize >= "1048576" Then
            Return ((MemBitSize / 1048576) & "MB")
        ElseIf MemBitSize >= "1073741824" Then

        End If
        Return "0"
    End Function
End Class

See if this helps.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x As New Memory
        MsgBox(x.GetMemory)
        End
    End Sub
End Class

Public Class Memory
    Public Function GetMemory() As String
        Dim MemBitSize As String = CStr(My.Computer.Info.TotalPhysicalMemory)
        Select Case CDec(MemBitSize)
            Case 0 To CDec(999.999)
                MemBitSize = Format(CInt(CDec(MemBitSize)), "###,###,###,###,##0 bytes")
            Case 1000 To CDec(999999.999)
                MemBitSize = Format(CInt(CDec(MemBitSize) / 1024), "###,###,###,##0 KB")
            Case 1000000 To CDec(999999999.999)
                MemBitSize = Format(CInt(CDec(MemBitSize) / 1024 / 1024), "###,###,##0 MB")
            Case Is >= 1000000000
                MemBitSize = Format(CInt(CDec(MemBitSize) / 1024 / 1024 / 1024), "#,###.00 GB")
        End Select
        Return MemBitSize
    End Function
End Class

Dude, you are awesome. Thanks.

laso

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.