Vb.net Visual Basic物理内存

Vb.net Visual Basic物理内存,vb.net,visual-studio,memory,ram,Vb.net,Visual Studio,Memory,Ram,我正试图通过Visual Basic获取PC上安装的物理内存量(总RAM)。问题是,我得到的是返回“0字节”。此外,我还将尝试获取使用百分比、可用RAM量、总分页、可用页以及显示RAM资源监视器等使用情况的图形,在Windows中。问题是我无法首先获得正确数量的可用RAM以与其他内存一起前进 我做错了什么? 多谢各位 这是我的代码: Option Strict On Option Explicit On Imports System.Math Imports System.Management

我正试图通过Visual Basic获取PC上安装的物理内存量(总RAM)。问题是,我得到的是返回“0字节”。此外,我还将尝试获取使用百分比、可用RAM量、总分页、可用页以及显示RAM资源监视器等使用情况的图形,在Windows中。问题是我无法首先获得正确数量的可用RAM以与其他内存一起前进

我做错了什么? 多谢各位

这是我的代码:

Option Strict On
Option Explicit On
Imports System.Math
Imports System.Management
Imports System.Runtime.InteropServices
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " API "
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Private Structure MEMORYSTATUSEX
        Dim dwLength As Integer
        Dim dwMemoryLoad As Integer
        Dim ullTotalPhys As ULong
    End Structure
    Private memoryInfo As MEMORYSTATUSEX
    Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32" (ByRef lpBuffer As MEMORYSTATUSEX)
#End Region


#Region " Variables "

    Private mullTotalRAM As ULong

#End Region

#Region " Form Events "
    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        ' set title
        Me.Text = My.Application.Info.Title & " " & My.Application.Info.Version.Major.ToString & "." & _
            My.Application.Info.Version.Minor.ToString

        Application.DoEvents()
        GetMemoryInfo()
        Timer1.Enabled = True
    End Sub

#End Region

#Region " Information Gathering and Display "

    Private Sub GetMemoryInfo()

        System.Windows.Forms.Application.DoEvents()

        ' set size of structure (required by this api call)
        memoryInfo.dwLength = Marshal.SizeOf(memoryInfo)
        GlobalMemoryStatusEx(memoryInfo)

        mullTotalRAM = memoryInfo.ullTotalPhys

        txtRAM.Text = FormatBytes(mullTotalRAM)

    End Sub

#End Region

#Region " Update Timer "

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        GetMemoryInfo()

        Application.DoEvents()

    End Sub

#End Region


#Region " Formatting Routines "

    Private Function FormatBytes(ByVal ullBytes As ULong) As String
        Dim dblTemp As Double

        Try
            Select Case ullBytes
                Case Is >= 1073741824 'GB
                    dblTemp = CDbl(ullBytes / 1073741824)
                    Return FormatNumber(dblTemp, 2) & " GB"
                Case 1048576 To 1073741823
                    dblTemp = CDbl(ullBytes / 1048576) 'MB
                    Return FormatNumber(dblTemp, 0) & " MB"
                Case 1024 To 1048575
                    dblTemp = CDbl(ullBytes / 1024) 'KB
                    Return FormatNumber(dblTemp, 0) & " KB"
                Case 0 To 1023
                    dblTemp = ullBytes ' bytes
                    Return FormatNumber(dblTemp, 0) & " bytes"
                Case Else
                    Return ""
            End Select
        Catch
            Return ""
        End Try

    End Function


#End Region


    Private Sub ramaTotalRAM_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ramaTotalRAM.Enter

    End Sub

    Private Sub txtRAM_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRAM.TextChanged

    End Sub
End Class
选项严格打开
选项显式打开
导入系统。数学
导入系统。管理
导入System.Runtime.InteropServices
公开课表格1
继承System.Windows.Forms.Form
#地区“空气污染指数”
_
私有结构MemoryStatutex
将dwLength设置为整数
Dim dwMemoryLoad为整数
暗淡的乌龙
端部结构
作为MemoryStatuex的私有memoryInfo
私有声明自动子GlobalMemoryStatusEx库“kernel32”(ByRef lpBuffer作为MEMORYSTATUSEX)
#末端区域
#区域“变量”
乌龙私人酒店
#末端区域
#区域“形式事件”
私有子表单1_Load(ByVal eventSender作为System.Object,ByVal eventArgs作为System.eventArgs)处理MyBase.Load
"定题",
Me.Text=My.Application.Info.Title&&My.Application.Info.Version.Major.ToString&&_
My.Application.Info.Version.Minor.ToString
Application.DoEvents()
GetMemoryInfo()
Timer1.Enabled=True
端接头
#末端区域
#区域“信息收集和显示”
私有子GetMemoryInfo()
System.Windows.Forms.Application.DoEvents()
'设置结构的大小(此api调用需要)
memoryInfo.dwLength=Marshal.SizeOf(memoryInfo)
GlobalMemoryStatusEx(memoryInfo)
mullTotalRAM=memoryInfo.ullTotalPhys
Text=FormatBytes(总内存)
端接头
#末端区域
#区域“更新计时器”
私有子计时器1_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)
GetMemoryInfo()
Application.DoEvents()
端接头
#末端区域
#区域“格式化例程”
私有函数FormatBytes(ByVal ullBytes作为ULong)作为字符串
双精度dblTemp
尝试
选择大小写字节
大小写>=1073741824'GB
dblTemp=CDbl(ullBytes/1073741824)
返回格式编号(dblTemp,2)和“GB”
案件1048576至1073741823
dblTemp=CDbl(ullBytes/1048576)'MB
返回格式编号(dblTemp,0)和“MB”
案件1024至1048575
dblTemp=CDbl(ullBytes/1024)“KB
返回格式编号(dblTemp,0)和“KB”
案例0至1023
dblTemp=ullBytes'字节
返回格式编号(dblTemp,0)和“字节”
其他情况
返回“”
结束选择
抓住
返回“”
结束尝试
端函数
#末端区域
私有子ramaTotalRAM_Enter(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理ramaTotalRAM.Enter
端接头
私有子txtRAM_TextChanged(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理txtRAM.TextChanged
端接头
末级
我已经解决了这个问题。现在我想知道是否有可能制作这样的东西: 。我该怎么做呢。非常感谢。

这里是一个工作示例

通过使用
GlobalMemoryStatusEx
的定义和下面的
MEMORYSTATUSEX

Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32" (<[In](), Out()> lpBuffer As MEMORYSTATUSEX)
然后评论出来

 'memoryInfo.dwLength = CUInt(Marshal.SizeOf(memoryInfo)) 
我不知道你的结构定义是从哪里来的,但根据Pinvoke.net,它应该是

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Class MEMORYSTATUSEX

    ''' <summary>
    ''' Initializes a new instance of the <see cref="T:MEMORYSTATUSEX" /> class.
    ''' </summary>
    Public Sub New()
    Me.dwLength = CType(Marshal.SizeOf(GetType(MEMORYSTATUSEX)), UInt32)
    End Sub
    ' Fields
    ''' <summary>
    ''' Size of the structure, in bytes. You must set this member before calling GlobalMemoryStatusEx.
    ''' </summary>
    Public dwLength As UInt32
    ''' <summary>
    ''' Number between 0 and 100 that specifies the approximate percentage of physical memory that is in use (0 indicates no memory use and 100 indicates full memory use).
    ''' </summary>
    Public dwMemoryLoad As UInt32
    ''' <summary>
    ''' Total size of physical memory, in bytes.
    ''' </summary>
    Public ullTotalPhys As UInt64
    ''' <summary>
    ''' Size of physical memory available, in bytes.
    ''' </summary>
    Public ullAvailPhys As UInt64
    ''' <summary>
    ''' Size of the committed memory limit, in bytes. This is physical memory plus the size of the page file, minus a small overhead.
    ''' </summary>
    Public ullTotalPageFile As UInt64
    ''' <summary>
    ''' Size of available memory to commit, in bytes. The limit is ullTotalPageFile.
    ''' </summary>
    Public ullAvailPageFile As UInt64
    ''' <summary>
    ''' Total size of the user mode portion of the virtual address space of the calling process, in bytes.
    ''' </summary>
    Public ullTotalVirtual As UInt64
    ''' <summary>
    ''' Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.
    ''' </summary>
    Public ullAvailVirtual As UInt64
    ''' <summary>
    ''' Size of unreserved and uncommitted memory in the extended portion of the virtual address space of the calling process, in bytes.
    ''' </summary>
    Public ullAvailExtendedVirtual As UInt64
End Class

一些简单的单行程序来完成工作:

TotalPhysicalMemory

可用物理内存

TotalVirtualMemory

可用虚拟存储器

将您的API decl更改为此(请注意,您的代码中缺少
.dll


你在运行什么操作系统?这也不是VB,但可能是VB.Net。@Bob Riemersma:我在使用visual studio 2008,这是一个.VB应用程序(“Form1.VB”是它的名字)。@Eric J:我在运行Windows 7。该死的,VB.Net不是VB。通过清晰地标记,您可以更快地得到答案。我有一个类似的应用程序,它用于MemoryStatuex,它可以工作,它显示了正确的RAM量,但在我的情况下,它无法工作。我刚刚完成了显示可用RAM的部分,但仍然是“0字节”因此,在我在web上找到的类似应用程序中,它显示了正确的数量。是关于显示输出的对话框的内容。它被设置为只读:true、接受返回:true和修改器:public。@TeoRoxy添加了一些附加信息。我无法使用您的结构使您的代码正常工作,我使用了我发布的结构,甚至在那时我不得不更改GlobalMemoryStatusEX声明。因此,上面的方法确实有效,我不知道为什么你的方法不起作用。非常感谢。我注意到,如果我删除案例0到1023的部分,我的应用程序在运行时不会返回任何内容。如果我离开它,我会得到“0字节”。我觉得那里出了问题,但我不知道是什么。我作为模型使用的应用程序仍然像一个有着相同代码的魔咒一样工作。再次感谢您。@TeoRoxy您可以随时为这个问题添加注释,我会看到的。但是,如果这是一个新问题,最好是问一个新问题,因为有很多非常优秀的程序员。请记住,当有人回答您的问题并将答案标记为已接受时,这将关闭问题并为您和他们提供一些额外的代表。通过在API声明“kernel32.dll”中进行更改,它仍然无法工作。上面的代码,是的,它可以工作,非常感谢。我也尝试过
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Class MEMORYSTATUSEX

    ''' <summary>
    ''' Initializes a new instance of the <see cref="T:MEMORYSTATUSEX" /> class.
    ''' </summary>
    Public Sub New()
    Me.dwLength = CType(Marshal.SizeOf(GetType(MEMORYSTATUSEX)), UInt32)
    End Sub
    ' Fields
    ''' <summary>
    ''' Size of the structure, in bytes. You must set this member before calling GlobalMemoryStatusEx.
    ''' </summary>
    Public dwLength As UInt32
    ''' <summary>
    ''' Number between 0 and 100 that specifies the approximate percentage of physical memory that is in use (0 indicates no memory use and 100 indicates full memory use).
    ''' </summary>
    Public dwMemoryLoad As UInt32
    ''' <summary>
    ''' Total size of physical memory, in bytes.
    ''' </summary>
    Public ullTotalPhys As UInt64
    ''' <summary>
    ''' Size of physical memory available, in bytes.
    ''' </summary>
    Public ullAvailPhys As UInt64
    ''' <summary>
    ''' Size of the committed memory limit, in bytes. This is physical memory plus the size of the page file, minus a small overhead.
    ''' </summary>
    Public ullTotalPageFile As UInt64
    ''' <summary>
    ''' Size of available memory to commit, in bytes. The limit is ullTotalPageFile.
    ''' </summary>
    Public ullAvailPageFile As UInt64
    ''' <summary>
    ''' Total size of the user mode portion of the virtual address space of the calling process, in bytes.
    ''' </summary>
    Public ullTotalVirtual As UInt64
    ''' <summary>
    ''' Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.
    ''' </summary>
    Public ullAvailVirtual As UInt64
    ''' <summary>
    ''' Size of unreserved and uncommitted memory in the extended portion of the virtual address space of the calling process, in bytes.
    ''' </summary>
    Public ullAvailExtendedVirtual As UInt64
End Class
Dim info As Microsoft.VisualBasic.Devices.ComputerInfo = New Microsoft.VisualBasic.Devices.ComputerInfo 
Debug.Print(CStr(info.TotalPhysicalMemory))
Debug.Print(CStr(info.TotalVirtualMemory))
Debug.Print(CStr(info.AvailablePhysicalMemory))
MsgBox(String.Format("TotalPhysicalMemory: {0} MBytes", System.Math.Round(My.Computer.Info.TotalPhysicalMemory / (1024 * 1024)), 2).ToString)
    MsgBox(String.Format("AvailablePhysicalMemory: {0} MBytes", System.Math.Round(My.Computer.Info.AvailablePhysicalMemory / (1024 * 1024)), 2).ToString)
    MsgBox(String.Format("TotalVirtualMemory: {0} MBytes", System.Math.Round(My.Computer.Info.TotalVirtualMemory / (1024 * 1024)), 2).ToString)
    MsgBox(String.Format("AvailableVirtualMemory: {0} MBytes", System.Math.Round(My.Computer.Info.AvailableVirtualMemory / (1024 * 1024)), 2).ToString)
Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32.dll" (ByRef lpBuffer As MEMORYSTATUSEX)
Friend Sub ReleaseMemory()
        Try
            GC.Collect()
            GC.WaitForPendingFinalizers()
            If Environment.OSVersion.Platform = PlatformID.Win32NT Then
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1)
            End If
        Catch ex As Exception
            LogError(ex.ToString())
        End Try
    End Sub