C# 獲取內存和CPU信息的代碼
獲取cpu信息
using System.Collections; using System.Collections.Generic; using System.Management; using System.Text; namespace MergeSystem.Indexus.WinServiceCommon.SystemManagement { /// <summary> /// Represents the CPU scope information /// </summary> 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 { /// <summary> /// Represents the Memory scope information /// </summary> 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(@" --- "); } } } }
本文由用戶 wen5 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!