C# 如何获取电脑CPU使用情况

 时间:2024-11-03 01:31:54

CPU是是电脑的中央处理器。要想用C#获取使用情况也很简单的。

工具/原料

电脑

方法/步骤

1、获取内存使用率方式一:输入代码:usingSy衡痕贤伎stem;usingSystem.Runtime.InteropServices;na罪焐芡拂mespaceConsoleApp1{publicclassClass1{staticvoidMain(string[]args){Console.WriteLine("总内存:"+FormatSize(GetTotalPhys()));Console.WriteLine("已使用:"+FormatSize(GetUsedPhys()));Console.WriteLine("可使用:"+FormatSize(GetAvailPhys()));Console.ReadKey();}#region获得内存信息API[DllImport("kernel32.dll")][return:MarshalAs(UnmanagedType.Bool)]publicstaticexternboolGlobalMemoryStatusEx(refMEMORY_INFOmi);//定义内存的信息结构[StructLayout(LayoutKind.Sequential)]publicstructMEMORY_INFO{publicuintdwLength;//当前结构体大小publicuintdwMemoryLoad;//当前内存使用率publiculongullTotalPhys;//总计物理内存大小publiculongullAvailPhys;//可用物理内存大小publiculongullTotalPageFile;//总计交换文件大小publiculongullAvailPageFile;//总计交换文件大小publiculongullTotalVirtual;//总计虚拟内存大小publiculongullAvailVirtual;//可用虚拟内存大小publiculongullAvailExtendedVirtual;//保留这个值始终为0}#endregion#region格式化容量大小///<summary>///格式化容量大小///</summary>///<paramname="size">容量(B)</param>///<returns>已格式化的容量</returns>privatestaticstringFormatSize(doublesize){doubled=(double)size;inti=0;while((d>1024)&&(i<5)){d/=1024;i++;}string[]unit={"B","KB","MB","GB","TB"};return(string.Format("{0}{1}",Math.Round(d,2),unit[i]));}#endregion#region获得当前内存使用情况///<summary>///获得当前内存使用情况///</summary>///<returns></returns>publicstaticMEMORY_INFOGetMemoryStatus(){MEMORY_INFOmi=newMEMORY_INFO();mi.dwLength=(uint)System.Runtime.InteropServices.Marshal.SizeOf(mi);GlobalMemoryStatusEx(refmi);returnmi;}#endregion#region获得当前可用物理内存大小///<summary>///获得当前可用物理内存大小///</summary>///<returns>当前可用物理内存(B)</returns>publicstaticulongGetAvailPhys(){MEMORY_INFOmi=GetMemoryStatus();returnmi.ullAvailPhys;}#endregion#region获得当前已使用的内存大小///<summary>///获得当前已使用的内存大小///</summary>///<returns>已使用的内存大小(B)</returns>publicstaticulongGetUsedPhys(){MEMORY_INFOmi=GetMemoryStatus();return(mi.ullTotalPhys-mi.ullAvailPhys);}#endregion#region获得当前总计物理内存大小///<summary>///获得当前总计物理内存大小///</summary>///<returns&gt;总计物理内存大小(B)&lt;/returns&gt;publicstaticulongGetTotalPhys(){MEMORY_INFOmi=GetMemoryStatus();returnmi.ullTotalPhys;}#endregion}}

2、方式2:备注:需要添加System.Managem娣定撰钠ent的引用。usingSystem;usingS烤恤鹇灭ystem.Management;namespaceConsoleApp1{publicclassClass1{staticvoidMain(string[]args){//需要添加System.Management的引用//获取总物理内存大小ManagementClasscimobject1=newManagementClass("Win32_PhysicalMemory");ManagementObjectCollectionmoc1=cimobject1.GetInstances();doubleavailable=0,capacity=0;foreach(ManagementObjectmo1inmoc1){capacity+=((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString())/1024/1024/1024.0,1)));}moc1.Dispose();cimobject1.Dispose();//获取内存可用大小ManagementClasscimobject2=newManagementClass("Win32_PerfFormattedData_PerfOS_Memory");ManagementObjectCollectionmoc2=cimobject2.GetInstances();foreach(ManagementObjectmo2inmoc2){available+=((Math.Round(Int64.Parse(mo2.Properties["AvailableMBytes"].Value.ToString())/1024.0,1)));}moc2.Dispose();cimobject2.Dispose();Console.WriteLine("总内存="+capacity.ToString()+"G");Console.WriteLine("可使用="+available.ToString()+"G");Console.WriteLine("已使用="+((capacity-available)).ToString()+"G,"+(Math.Round((capacity-available)/capacity*100,0)).ToString()+"%");Console.ReadKey();}}}

3、获取CPU使用率usingSystem;usingSystem.Diagnostics;namespaceConsoleApp1{publicclassClass1{staticvoidMain(string[]args){PerformanceCountercpuCounter;PerformanceCounterramCounter;cpuCounter=newPerformanceCounter();cpuCounter.CategoryName="Processor";cpuCounter.CounterName="%ProcessorTime";cpuCounter.InstanceName="_Total";cpuCounter=newPerformanceCounter("Processor","%ProcessorTime","_Total");ramCounter=newPerformanceCounter("Memory","AvailableMBytes");Console.WriteLine("电脑CPU使用率:"+cpuCounter.NextValue()+"%");Console.WriteLine("电脑可使用内存:"+ramCounter.NextValue()+"MB");Console.WriteLine();while(true){System.Threading.Thread.Sleep(1000);Console.WriteLine("电脑CPU使用率:"+cpuCounter.NextValue()+"%");Console.WriteLine("电脑可使用内存:"+ramCounter.NextValue()+"MB");Console.WriteLine();if((int)cpuCounter.NextValue()>80){System.Threading.Thread.Sleep(1000*60);}}}}}

脱沙的翡翠原石预示着什么?看懂就是财富 学习乐器该注意什么才不会少走弯路? 牙签肉的做法 玉簪黄叶的原因和解决办法 国产手机更改默认桌面
热门搜索
燕隼图片 国庆节手抄报图片大全简单又漂亮 eva同人漫画 探照灯图片 邪恶漫画孙尚香无惨