基于C#编写获取硬件信息的工具类
写在前面
通过性能计数器类 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 (ManagempythonentObject 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.CoiTErHPntains("PCI")) { hdInfo.NetworkcardName = S1; } } } } catch (ManagementException ex) { hdInfo.NetworkcardName = "未知"; } #endregion #region 生产商和生产日期 try { http://www.devze.com //获取生产商 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 = "未知"; } #endregion return 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; } }
到此这篇关于基于C#编写获取硬件信息的工具类的文章就介绍到这了,更多相关C#获取硬件信息内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论