资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#获取内存和CPU信息的源码

将内容过程中经常用的一些内容段备份一下,如下资料是关于C# 获取内存和CPU信息的的内容,希望能对大伙有一些用。

涵江ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

using System.Collections;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace MergeSystem.Indexus.WinServiceCommon.SystemManagement
{
    public class Cpu
    {
        public static void LogCpuData()
        {
            System.Collections.ArrayList result = new System.Collections.ArrayList();
            ManagementScope mgmtScope = new ManagementScope(@"/. ootcimv2");
            mgmtScope.Connect();

            ManagementPath mp = new ManagementPath("Win32_Processor");
            ManagementClass mc = new ManagementClass(mgmtScope, mp, null);
            ManagementObjectCollection procs = mc.GetInstances();

            foreach (ManagementObject mo in procs)
            {
                foreach (PropertyData pd in mo.Properties)
                {
                    switch (pd.Name)
                    {
                        case "DeviceID":
                        case "Name":
                        case "LoadPercentage":
                            Console.WriteLine(@"Name: {0}; Value: {1}", pd.Name, pd.Value == null ? string.Empty : pd.Value.ToString().Trim());
                            Handler.LogHandler.Info(string.Format(@"Name: {0}; Value: {1}", pd.Name, pd.Value == null ? string.Empty : pd.Value.ToString().Trim()));
                            Handler.LogHandler.MemoryFatalException(string.Format(@"Name: {0}; Value: {1}", pd.Name, pd.Value == null ? string.Empty : pd.Value.ToString().Trim()));

                            break;
                    }
                }
                Console.WriteLine(@" --- ");
                Handler.LogHandler.Info(@" --- ");
            }
        }
    }
}

获取内存信息

using System;
using System.Collections;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace MergeSystem.Indexus.WinServiceCommon.SystemManagement
{
    public class Memory
    {

        public static void LogMemoryData()
        {
            ManagementScope mgmtScope = new ManagementScope(@"/. ootcimv2");
            mgmtScope.Connect();

            ManagementPath mp = new ManagementPath("Win32_OperatingSystem");
            ManagementClass mc = new ManagementClass(mgmtScope, mp, null);
            ManagementObjectCollection procs = mc.GetInstances();

            foreach (ManagementObject mo in procs)
            {
                foreach (PropertyData pd in mo.Properties)
                {
                    switch (pd.Name)
                    {
                        case "BuildNumber":
                        case "BuildType":
                        case "Caption":
                        case "CountryCode":
                        case "CSDVersion":
                        case "CSName":
                        case "CurrentTimeZone":
                        case "FreePhysicalMemory":
                        case "FreeSpaceInPagingFiles":
                        case "FreeVirtualMemory":
                        case "LastBootUpTime":
                        case "MaxNumberOfProcesses":
                        case "MaxProcessMemorySize":
                        case "NumberOfProcesses":
                        case "NumberOfUsers":
                        case "Organization":
                        case "OSLanguage":
                        case "RegisteredUser":
                        case "SerialNumber":
                        case "ServicePackMajorVersion":
                        case "ServicePackMinorVersion":
                        case "SizeStoredInPagingFiles":
                        case "SystemDevice":
                        case "SystemDirectory":
                        case "SystemDrive":
                        case "TotalVirtualMemorySize":
                        case "TotalVisibleMemorySize":
                        case "Version":
                        case "WindowsDirectory":
                            Console.WriteLine(@"Name: {0}; Value: {1}", pd.Name, pd.Value == null ? string.Empty : pd.Value.ToString().Trim());
                            Handler.LogHandler.Info(string.Format(@"Name: {0}; Value: {1}", pd.Name, pd.Value == null ? string.Empty : pd.Value.ToString().Trim()));
                            Handler.LogHandler.MemoryFatalException(string.Format(@"Name: {0}; Value: {1}", pd.Name, pd.Value == null ? string.Empty : pd.Value.ToString().Trim()));
                            break;
                    }                    
                }
                Console.WriteLine(@" --- ");
                Handler.LogHandler.Info(@" --- ");
            }

        }
    }
}

新闻标题:C#获取内存和CPU信息的源码
网页网址:http://cdkjz.cn/article/gjgcsi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220