重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

C#获取内存和CPU信息的源码-创新互联

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

成都创新互联公司主要从事成都网站建设、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务金水,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
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(@" --- ");
            }

        }
    }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:C#获取内存和CPU信息的源码-创新互联
网页URL:http://cqcxhl.cn/article/cooicd.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP