1,获取当前CPU使用率(需添加System.Management.dll):
string queryStr = "select * from win32_processor";System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(queryStr);//假设只有一个CPUdouble loadPercentage = 0;//CPU使用率(百分比的值)
foreach (System.Management.ManagementObject item in searcher.Get())
{loadPercentage = double.Parse(item["LoadPercentage"].ToString());
}
2,获取当前内存使用率(需添加 Microsoft.VisualBasic.dll):
Microsoft.VisualBasic.Devices.ComputerInfo pc = new Microsoft.VisualBasic.Devices.ComputerInfo();textBox1.Text = pc.TotalPhysicalMemory / 1024 / 1024 + "M";//总物理内存textBox2.Text = pc.AvailablePhysicalMemory / 1024 / 1024 + "M";//可用物理内存textBox3.Text = pc.TotalVirtualMemory / 1024 / 1024 + "M";//总虚拟内存textBox4.Text = pc.AvailableVirtualMemory / 1024 / 1024 + "M";//可用虚拟内存
3,其他常用WMI查询语句: