真爱无限的CSDN博客

专注于技术方面的交流与分享,提升专业能力!

.NET获取当前程序所在电脑的CPU和内存使用率


using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices; 

namespace ConsoleApplication1
{

    class Program
    {
        public static PerformanceCounter cpu;
        //public static ComputerInfo cif;
        static void Main(string[] args)
        {
            cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
           // cif = new ComputerInfo();
            MEMORY_INFO MemInfo;
            MemInfo = new MEMORY_INFO();
            while(true)
            {
                GlobalMemoryStatus(ref  MemInfo);
                Console.WriteLine(MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用");
                var percentage = cpu.NextValue();
                Console.WriteLine(percentage + "%的CPU正在使用\n");
                System.Threading.Thread.Sleep(2000);
            }


        }
        [DllImport("kernel32")]
        public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemInfo(ref  CPU_INFO cpuinfo);
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref  MEMORY_INFO meminfo);
        [DllImport("kernel32")]
        public static extern void GetSystemTime(ref  SYSTEMTIME_INFO stinfo);  
    }
    //定义CPU的信息结构  
    [StructLayout(LayoutKind.Sequential)]
    public struct CPU_INFO
    {
        public uint dwOemId;
        public uint dwPageSize;
        public uint lpMinimumApplicationAddress;
        public uint lpMaximumApplicationAddress;
        public uint dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public uint dwProcessorLevel;
        public uint dwProcessorRevision;
    }
    //定义内存的信息结构  
    [StructLayout(LayoutKind.Sequential)]
    public struct MEMORY_INFO
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public uint dwTotalPhys;
        public uint dwAvailPhys;
        public uint dwTotalPageFile;
        public uint dwAvailPageFile;
        public uint dwTotalVirtual;
        public uint dwAvailVirtual;
    }
    //定义系统时间的信息结构  
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME_INFO
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;
    } 
}


可以循环判断cpu和内存使用率,来做相应的功能,比如迅雷的空闲下载就和这个差不多的,就是电脑cpu和内存都很闲的时候,做一些要做的工作,以免人在使用的时候同时运行造成电脑卡机。

阅读更多
版权声明:作者:真爱无限 出处:http://blog.csdn.net/pukuimin1226 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接. https://blog.csdn.net/pukuimin1226/article/details/46958487
个人分类: C#/.NET/VS/Linq
上一篇笔记20150522-生成更新重复单据编号的update语句
下一篇EntityFramework.BulkInsert扩展插入数据和EF本身插入数据比较
想对作者说点什么? 我来说一句

c# 获取系统内存和cpu使用情况

2010年11月16日 48KB 下载

没有更多推荐了,返回首页

关闭
关闭