原出處
***詳細還請至原作者那兒***
-------
' 匯入名稱空間
Imports System.Diagnostics
Imports System.Management
Public Class Form1
' Processor
Private Shared CPU_Core1 As PerformanceCounter = Nothing
Private Shared CPU_Core2 As PerformanceCounter = Nothing
' Page File
Private Shared PageFile_Usage As PerformanceCounter = Nothing
Private Shared PageFile_UsagePeak As PerformanceCounter = Nothing
' My.Computer.Info
Private Shared cptInfo As Devices.ComputerInfo = Nothing
' 查詢擷取管理物件
Private Shared Searcher As ManagementObjectSearcher = Nothing
Private Shared TotPhyMem As UInt64 = 0 ' Total Physical Memory
Private WithEvents tmr As New Timer ' Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' 宣告並建立 "效能計數器元件" 類別
' New PerformanceCounter(CategoryName,CounterName,InstanceName)
' CategoryName : 取得或設定這個效能計數器的效能計數器分類的名稱。
' CounterName : 取得或設定與這個 PerformanceCounter 執行個體相關的效能計數器的名稱。
' InstanceName : 取得或設定這個效能計數器的執行個體名稱。
' 可用 Environment.ProcessorCount 取得 CPU Core 核心數量
' Processor Core#0 核心1
CPU_Core1 = New PerformanceCounter("Processor", "% Processor Time", "0")
' Processor Core#1 核心2
CPU_Core2 = New PerformanceCounter("Processor", "% Processor Time", "1")
' 分頁檔
PageFile_Usage = New PerformanceCounter("Paging File", "% Usage", "_Total")
' 尖峰分頁檔
PageFile_UsagePeak = New PerformanceCounter("Paging File", "% Usage Peak", "_Total")
cptInfo = New Devices.ComputerInfo() ' 取電腦資訊
tmr.Interval = 999 ' Timer.Interval 屬性 : 取得或設定引發 Elapsed 事件的間隔。
tmr.Start() ' 啟動 Timer
' 取得 CPU 名稱 (WMI)
For Each MngObj As ManagementObject In New ManagementObjectSearcher(New SelectQuery("Win32_Processor")).Get()
' Get Processor Name
imgLstBoxCPU.Items.Add(String.Format(" CPU : {0}", MngObj.GetPropertyValue("Name")), 1)
Next
GetProcessorInfo() ' 取得 CPU 資訊 (WMI)
' My.Computer.Info.TotalPhysicalMemory
TotPhyMem = cptInfo.TotalPhysicalMemory ' 取得全部實體記憶體
lbl實體記憶體.Text = KB2MB(TotPhyMem) ' 顯示到 Label
End Sub
Private Sub GetProcessorInfo() ' 取得 CPU 資訊 (WMI)
'使用 SelectQuery 物件可下 SELECT 陳述式查詢
'Select * From Win32_Processor 語法可省略 , 直接寫 WMI 類別名稱即可
Dim SelQry As New SelectQuery("Win32_Processor")
'使用 ManagementObjectSearcher 物件取回一個 WMI 物件基底之查詢的 集合物件(Collection)
Using MngObjSch As New ManagementObjectSearcher(SelQry)
' 使用 ManagementObjectSearcher.Get 方法, 叫用指定的 WMI 查詢,並傳回產生的集合。
' 列舉出 Win32_Processor WMI 類別中每一個處理器 (成員)
For Each MngObj As ManagementObject In MngObjSch.Get()
' 顯示 CPU 資訊
lstBox.Items.Add(String.Format("Caption : {0}", MngObj!Caption))
lstBox.Items.Add(String.Format("Manufacturer : {0}", MngObj!Manufacturer))
lstBox.Items.Add(String.Format("Number Of Logical Processors : {0}", MngObj!NumberOfLogicalProcessors))
lstBox.Items.Add(String.Format("L2 Cache Size : {0} KB", MngObj!L2CacheSize))
lstBox.Items.Add(String.Format("Current Clock Speed : {0} MHz", MngObj!CurrentClockSpeed))
lstBox.Items.Add(String.Format("Clock frequency : {0} MHz", MngObj!MaxClockSpeed))
lstBox.Items.Add(String.Format("CurrentVoltage : {0} V", MngObj!CurrentVoltage))
lstBox.Items.Add(String.Format("Number Of Cores : {0}", MngObj!NumberOfCores))
lstBox.Items.Add(String.Format("Address Width : {0}", MngObj!AddressWidth))
lstBox.Items.Add(String.Format("External Clock : {0} MHz", MngObj!ExtClock))
lstBox.Items.Add(String.Format("Data Width : {0}", MngObj!DataWidth))
lstBox.Items.Add(String.Format("Socket Designation : {0}", MngObj!SocketDesignation))
Next
End Using
End Sub
' Timer Tick 事件
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
Dim Core1 As Int32 = dgt2(CPU_Core1.NextValue) ' 取得 CPU 核心1 使用率
Dim Core2 As Int32 = dgt2(CPU_Core2.NextValue) ' 取得 CPU 核心2 使用率
lblCore1Usage.Text = String.Format("Core #0 {0} % ", Core1) ' 取得 CPU 核心1 使用率
lblCore2Usage.Text = String.Format("Core #1 {0} % ", Core2) ' 取得 CPU 核心2 使用率
Dim AvPhyMem As UInt64 = cptInfo.AvailablePhysicalMemory ' 取得可用實體記憶體
Dim TotVtMem As UInt64 = cptInfo.TotalVirtualMemory ' 取得全部虛擬記憶體
Dim AvVtNen As UInt64 = cptInfo.AvailableVirtualMemory ' 取得可用虛擬記憶體
lblAvPhyMem.Text = KB2MB(AvPhyMem) ' 顯示可用實體記憶體
lblTotVtMem.Text = KB2MB(TotVtMem) ' 顯示全部虛擬記憶體
lblAvVtNen.Text = KB2MB(AvVtNen) ' 顯示可用虛擬記憶體
lblRAMusage.Text = KB2MB(TotPhyMem - AvPhyMem) ' 顯示 RAM 已用
' 顯示分頁檔使用
lblPageFileUsage.Text = String.Format(" Page File {0:##0.00} %", PageFile_Usage.NextValue)
' 顯示分頁檔尖峰使用
lblPageFileUsagePeak.Text = String.Format(" Page File Peak {0:##0.00} %", PageFile_UsagePeak.NextValue)
' 顯示本程式使用記憶體
' Process.GetCurrentProcess.WorkingSet
' Process.GetCurrentProcess.WorkingSet64
bsi.Caption = String.Format("本程式記憶體 : {0} MB", KB2MB(Environment.WorkingSet))
End Sub
Private Shared Function KB2MB(ByVal kb As Long) As String
' 講 KB 轉 MB ( / 1024 / 1024 ) 並指定格式
' 四捨五入將值捨入至指定的小數點兩位。
Return Math.Round(kb / 2 ^ 20, 2).ToString("#,##0.00")
End Function
Private Shared Function dgt2(ByVal value As Single) As String
' 四捨五入將值捨入至指定的小數點兩位。
Return Math.Round(value, 2).ToString()
End Function
End Class
沒有留言:
張貼留言