将内容过程中经常用的一些内容段备份一下,如下资料是关于C# 获取内存和CPU信息的的内容,希望能对大伙有一些用。
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(@" --- ");
}
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。