码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-04-21 18:45:58      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:load   ges   cond   dwl   max   process   void   layout   size   

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

 

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

标签:load   ges   cond   dwl   max   process   void   layout   size   

原文地址:http://www.cnblogs.com/test404/p/6744989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!