写在前面
通过性能计数器类 PerformanceCounter 传入计数器名称,获取对应的系统指标数据,以便进行分析和参考。
通过System.Management类库中的ManagementObjectSearcher类实现对管理信息的查询,比如:主板信息、内存信息、硬盘信息、显卡信息、声卡信息、网卡信息、生产商和生产日期等。
需引入一下命名空间:
using System.Diagnostics;
using System.Management;
using System.Net.NetworkInformation;
using System.Text.Json;
代码实现
public class DeviceInfoHelper{/// <summary>/// 获取CPU利用率/// </summary>public static float GetCpuStatus(){// 创建对 CPU 占用百分比的性能计数器。var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");if (cpuCounter != null){return cpuCounter.NextValue();}return 0;}/// <summary>/// 获取剩余内存/// </summary>public static float GetAvailableMemory(){// 创建内存占用字节数的性能计数器var ramCounter = new PerformanceCounter("Memory", "Available MBytes");if (ramCounter != null){return ramCounter.NextValue();}return 0;}/// <summary>/// 获取当前进程CPU和内存使用情况/// </summary>public static Tuple<float, float> GetCurrentProcessorUsage(){var processName = Process.GetCurrentProcess().ProcessName;var theCPUCounter = new PerformanceCounter("Process", "% Processor Time", processName);var theMemCounter = new PerformanceCounter("Process", "Working Set", processName);return new Tuple<float, float>(theCPUCounter.NextValue(), theMemCounter.NextValue());}/// <summary>/// 获取硬件信息摘要/// </summary>public static string GetHardwareSummary(){var hdInfo = new HardwardSummary{MainboardName = "未知",MemorySize = "未知",HarddiskSize = "未知",VideocardName = "未知",SoundcardName = "未知",NetworkcardName = "未知",ManufacturerName = "未知",ManufactureDate = "未知"};#region 主板信息try{ManagementObjectSearcher myMainboard = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");foreach (ManagementObject board in myMainboard.Get()){hdInfo.MainboardName = board["Product"].ToString();}}catch (Exception ex){hdInfo.MainboardName = "未知";}#endregion#region 内存信息try{ManagementObjectSearcher myMemory = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");//定义内存总大小变量double MemorySize = 0;foreach (ManagementObject obj in myMemory.Get()){//将内存大小换算成G单位MemorySize += Convert.ToDouble(obj["Capacity"]) / 1024 / 1024 / 1024;}hdInfo.MemorySize = MemorySize.ToString() + "G";}catch (ManagementException e){hdInfo.MemorySize = "未知";}#endregion#region 硬盘信息try{//获取本机所有硬盘信息 ManagementObjectSearcher myHarddisk = new ManagementObjectSearcher("select * from Win32_DiskDrive");foreach (ManagementObject drive in myHarddisk.Get()){//获取硬盘容量var capacity = (Convert.ToDouble(drive["Size"]) / 1024 / 1024 / 1024).ToString("0.00") + "G";//获取硬盘类型var mediaType = drive["MediaType"];hdInfo.HarddiskSize = capacity.ToString() + "|" + mediaType.ToString();}}catch (Exception ex){hdInfo.HarddiskSize = "未知";}#endregion#region 显卡信息try{ManagementObjectSearcher myVideoObject = new ManagementObjectSearcher("select * from Win32_VideoController");foreach (ManagementObject obj in myVideoObject.Get()){hdInfo.VideocardName = obj["Name"].ToString();}}catch (ManagementException ex){hdInfo.VideocardName = "未知";}#endregion#region 声卡信息try{// 创建WMI搜索对象ManagementObjectSearcher mySoundcard = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SoundDevice");// 遍历搜索结果foreach (ManagementObject mo in mySoundcard.Get()){// 获取声卡名称hdInfo.SoundcardName = mo.GetPropertyValue("Name").ToString();}}catch (ManagementException ex){hdInfo.SoundcardName = "未知";}#endregion#region 网卡信息try{// 获取本地网络接口信息 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();foreach (NetworkInterface adapter in nics){// 如果是RJ45网卡 if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet){string S1 = (string)adapter.Description;if (S1.Contains("PCI")){hdInfo.NetworkcardName = S1;}}}}catch (ManagementException ex){hdInfo.NetworkcardName = "未知";}#endregion#region 生产商和生产日期try{//获取生产商ManagementObjectSearcher ManufacturerInfo = new ManagementObjectSearcher("select * from Win32_ComputerSystem");foreach (ManagementObject obj in ManufacturerInfo.Get()){hdInfo.ManufacturerName = obj["Manufacturer"].ToString();}//获取生产日期ConnectionOptions options = new ConnectionOptions();ManagementScope scope = new ManagementScope("\\\\.\\root\\cimv2", options);ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_BIOS");ManagementObjectSearcher BoisInfo = new ManagementObjectSearcher(scope, query);foreach (ManagementObject mo in BoisInfo.Get()){string StrManufactureDate = mo["ReleaseDate"].ToString().Substring(0, 8);DateTime DT = DateTime.ParseExact(StrManufactureDate, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);hdInfo.ManufactureDate = String.Format("{0:d}", DT);}}catch (Exception ex){hdInfo.ManufacturerName = "未知";hdInfo.ManufactureDate = "未知";}#endregionreturn JsonSerializer.Serialize(hdInfo);}}public class HardwardSummary{public string MainboardName { get; set; }public string MemorySize { get; set; }public string HarddiskSize { get; set; }public string VideocardName { get; set; }public string SoundcardName { get; set; }public string NetworkcardName { get; set; }public string ManufacturerName { get; set; }public string ManufactureDate { get; set; }}