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

2017-08-05 10:33:24  访问(3638) 赞(0) 踩(0)


相关下载:SlowX.ExamMemory(执行程序)  SlowX.ExamMemory(源码)     


点评:可以获得内存的使用百分比,但没法获得CPU的。这个代码有缺陷

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

namespace SlowX.ExamMemory
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

          

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

        private void toolStripButton_获得内存信息_Click(object sender, EventArgs e)
        {
            try
            {

                PerformanceCounter cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                // cif = new ComputerInfo();  
                MEMORY_INFO MemInfo;
                MemInfo = new MEMORY_INFO();

                StringBuilder theResult = new StringBuilder();


                theResult.AppendLine("当前时间:" + DateTime.Now.ToString("HH:mm:ss"));
                GlobalMemoryStatus(ref  MemInfo);
                theResult.AppendLine(MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用");

                var percentage = cpu.NextValue();
                theResult.AppendLine(percentage + "%的CPU正在使用\n");

                theResult.AppendLine("其他cpu属性信息[可忽略不看]:");
                theResult.AppendLine("cpu.CategoryName:" + cpu.CategoryName);
                theResult.AppendLine("cpu.CounterHelp:" + cpu.CounterHelp);
                theResult.AppendLine("cpu.CounterHelp:" + cpu.CounterName);
                theResult.AppendLine("cpu.InstanceName:" + cpu.InstanceName);
                theResult.AppendLine("cpu.MachineName:" + cpu.MachineName);
                theResult.AppendLine("cpu.RawValue:" + cpu.RawValue.ToString());
                theResult.AppendLine("cpu.ReadOnly:" + cpu.ReadOnly.ToString());

                theResult.AppendLine();

                theResult.AppendLine("其他内存属性信息[可忽略不看]:");
                theResult.AppendLine("MemInfo.dwAvailPageFile:" + MemInfo.dwAvailPageFile.ToString());
                theResult.AppendLine("MemInfo.dwAvailPhys:" + MemInfo.dwAvailPhys.ToString());
                theResult.AppendLine("MemInfo.dwAvailVirtual:" + MemInfo.dwAvailVirtual.ToString());
                theResult.AppendLine("MemInfo.dwLength:" + MemInfo.dwLength.ToString());
                theResult.AppendLine("MemInfo.dwMemoryLoad:" + MemInfo.dwMemoryLoad.ToString());
                theResult.AppendLine("MemInfo.dwTotalPageFile:" + MemInfo.dwTotalPageFile.ToString());
                theResult.AppendLine("MemInfo.dwTotalPhys:" + MemInfo.dwTotalPhys.ToString());
                theResult.AppendLine("MemInfo.dwTotalVirtual:" + MemInfo.dwTotalVirtual.ToString());

                richTextBox1.Text = theResult.ToString();
            }
            catch (Exception err)
            {
                richTextBox1.Text = "异常:" + err.Message;
            }
        }
    }
}


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)