获取当前电脑的cpu使用率、内存使用率

本文介绍如何使用C#中的PerformanceCounter类监测CPU使用率,并利用ComputerInfo类获取详细的内存信息,包括总内存、虚拟内存、可用物理内存及可用虚拟内存的大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
        }
    }
}

 

转载于:https://www.cnblogs.com/wholeworld/p/10041893.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值