我想通過Visual Basic獲取我的PC上安裝的物理內存(總RAM)的數量。問題是我得到的返回「0字節」。此外,我還會嘗試喲得到使用的百分比,可用RAM的數量,總分頁,免費頁面和一個圖形顯示的使用情況,如資源監視器的RAM,在Windows.The問題是,我不能得到正確數量的可用RAM的第一與其他人一起前進。Visual Basic物理內存
我在做什麼錯了? 謝謝。
這是我的代碼:
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
我已經解決了這個problem.Now我想知道是否有可能讓這樣的事情: http://s18.postimage.org/7zn5adst3/Memory.jpg。怎麼能我也很it.thank你許多。
您運行的是什麼操作系統? –
這不是VB,但也許VB.Net。 – Bob77
@Bob Riemersma:我使用Visual Studio 2008,這是一個應用程序的.vb(「Form1.vb的」這個名字吧)。 –