获取系统的CPU使用率、内存使用率

获取系统的性能参数,可以通过Process、PerformanceCounter或者WMI完成。

下面看看PerformanceCounter类是怎么获取的。

首先看看GetCategoryNameList()函数,这个函数是自定义的,主要用来获取本机上的计数器列表

        //获取性能计数器类别列表
        public static void GetCategoryNameList()
        {
            PerformanceCounterCategory[] myCat2;
            myCat2 = PerformanceCounterCategory.GetCategories();
            for (int i = 0; i < myCat2.Length;i++ )
            {
                Console.WriteLine(myCat2[i].CategoryName.ToString());
            }
            
        }

再看另外一个函数,这个主要是获取某一个计数器下的性能计数器的名称

        //获取性能计数器类别下的实例的名称实例下性能计数器的名称
        public static void GetInstanceNameListAndCounterNameList(string CategoryName)
        {
            string[] instanceName;
            ArrayList counters = new ArrayList();
            PerformanceCounterCategory mycat = new PerformanceCounterCategory(CategoryName);
            try
            {
                instanceName = mycat.GetInstanceNames();
                if (instanceName.Length==0)
                {
                    counters.AddRange(mycat.GetCounters());
                }
                else
                {
                    for (int i = 0; i < instanceName.Length;i++ )
                    {
                        counters.AddRange(mycat.GetCounters(instanceName[i]));
                    }
                }
                for (int i = 0; i < instanceName.Length;i++ )
                {
                    Console.WriteLine(instanceName[i]);
                }
                Console.WriteLine("*************************");
                foreach (PerformanceCounter counter in counters)
                {
                    Console.WriteLine(counter.CounterName);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Unable to list the counter for this category");
            }
        }


有了上面的两个函数,我们就可以知道本机的计数器已对应的性能名称。

获取系统的CPU使用率、内存使用率_第1张图片

上图就是本机的计数器名称,当然是很多的了……


获取系统的CPU使用率、内存使用率_第2张图片

上图是对应的Memory的性能计数器的名称,我们怎么获取某一个性能参数呢?

//根据categoryName ,counterName,instanceName 获得性能情况显示
        private static void PerformanceCounterFun(string CategoryName, string InstanceName, string CounterName)
        {
            PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);

            while (true)
            {
                Thread.Sleep(1000);
                float cpuload = pc.NextValue();
                Console.WriteLine("cpu load------->" + cpuload + "%");
            }
        }

上述的函数中,我们就可以如下的形式调用即可:

PerformanceCounterFun("Processor", "_Total", "% Processor Time");

我们就能获取处理器的CPU使用率。(这里就不贴图了)


接下来我们看看怎么通过获取内存的使用情况,这个主要是通过WMI实现的。


ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                if (mo["TotalVisibleMemorySize"] != null)
                {
                    long xx = long.Parse(mo["TotalVisibleMemorySize"].ToString());
                    Console.WriteLine(xx);
                }

                if (mo["FreePhysicalMemory"]!=null)
                {
                    long availablebytes = long.Parse(mo["FreePhysicalMemory"].ToString());
                    Console.WriteLine(availablebytes);
                }
            }

上述代码就帮助我们获取到物理内存和可用内存。获取到的结果和我们使用任务管理看到的内存性能参数是一致的。

你可能感兴趣的:(C#)