获取当前电脑的cpu使用率、内存使用率
https://www.cnblogs.com/Chary/p/7771365.html
http://www.cnblogs.com/zl1991/p/4679461.html
要关注几个类 PerformanceCounter 用来针对cpu ;ComputerInfo 用来针对内存
using Microsoft.VisualBasic.Devices; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CpuAndMemory { class Program { //PerformanceCounter 性能计数器 public static PerformanceCounter cpu; //来自程序集 Microsoft.VisualBasic.dll 手动引用 public static ComputerInfo cInfo; static void Main(string[] args) { cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total"); //---------------内存相关--------------- cInfo = new ComputerInfo(); Console.WriteLine("当前计算机的总内存大小为"+cInfo.TotalPhysicalMemory/1024/1024+"G"); Console.WriteLine("当前计算机的虚拟内存大小为:"+cInfo.TotalVirtualMemory / 1024 / 1024 + "G"); Console.WriteLine("当前计算机的虚拟内存大小为:" + cInfo.TotalVirtualMemory); Console.WriteLine("当前计算机的可用物理内存大小为:" + cInfo.AvailablePhysicalMemory); Console.WriteLine("当前计算机的可用虚拟内存大小为:" + cInfo.AvailableVirtualMemory); //---------------内存相关--------------- Console.ReadKey(); } } }