【C#】获取电脑CPU、内存、屏幕、磁盘等信息

通过WMI类来获取电脑各种信息,参考文章:WMI_04_常见的WMI类的属性_wmi scsilogicalunit_fantongl的博客-CSDN博客

自己整理了获取电脑CPU、内存、屏幕、磁盘等信息的代码

 #region 系统信息/// <summary>/// 电脑信息/// </summary>public partial class ComputerInfo{/// <summary>/// 系统版本/// <para>示例:Windows 10 Enterprise</para>/// </summary>public static string OSProductName { get; } = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", 0);/// <summary>/// 操作系统版本/// <para>示例:Microsoft Windows 10.0.18363</para>/// </summary>public static string OSDescription { get; } = System.Runtime.InteropServices.RuntimeInformation.OSDescription;/// <summary>/// 操作系统架构(<see cref="Architecture">)/// <para>示例:X64</para>/// </summary>public static string OSArchitecture { get; } = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture.ToString();/// <summary>/// 获取系统信息/// </summary>/// <returns></returns>public static SystemInfo GetSystemInfo(){SystemInfo systemInfo = new SystemInfo();var osProductName = OSProductName.Trim().Replace(" ", "_");var osVersionNames = Enum.GetNames(typeof(OSVersion)).ToList();for (int i = 0; i < osVersionNames.Count; i++){var osVersionName = osVersionNames[i];if (osProductName.Contains(osVersionName)){systemInfo.OSVersion = (OSVersion)Enum.Parse(typeof(OSVersion), osVersionName);systemInfo.WindowsVersion = osProductName.Replace(osVersionName, "").Replace("_", " ");}}systemInfo.WindowsVersionNo = OSDescription;systemInfo.Architecture = OSArchitecture;return systemInfo;}}/// <summary>/// 系统信息/// </summary>public class SystemInfo{/// <summary>/// 系统版本。如:Windows 10/// </summary>public string WindowsVersion { get; set; }/// <summary>/// 系统版本。如:专业版/// </summary>public OSVersion OSVersion { get; set; }/// <summary>/// Windows版本号。如:Microsoft Windows 10.0.18363/// </summary>public string WindowsVersionNo { get; set; }/// <summary>/// 操作系统架构。如:X64/// </summary>public string Architecture { get; set; }}/// <summary>/// 系统客户版本/// </summary>public enum OSVersion{/// <summary>/// 家庭版/// </summary>Home,/// <summary>/// 专业版,以家庭版为基础/// </summary>Pro,Professional,/// <summary>/// 企业版,以专业版为基础/// </summary>Enterprise,/// <summary>/// 教育版,以企业版为基础/// </summary>Education,/// <summary>/// 移动版/// </summary>Mobile,/// <summary>/// 企业移动版,以移动版为基础/// </summary>Mobile_Enterprise,/// <summary>/// 物联网版/// </summary>IoT_Core,/// <summary>/// 专业工作站版,以专业版为基础/// </summary>Pro_for_Workstations}#endregion#region CPU信息public partial class ComputerInfo{/// <summary>/// CPU信息/// </summary>/// <returns></returns>public static CPUInfo GetCPUInfo(){var cpuInfo = new CPUInfo();var cpuInfoType = cpuInfo.GetType();var cpuInfoFields = cpuInfoType.GetProperties().ToList();var moc = new ManagementClass("Win32_Processor").GetInstances();foreach (var mo in moc){foreach (var item in mo.Properties){if (cpuInfoFields.Exists(f => f.Name == item.Name)){var p = cpuInfoType.GetProperty(item.Name);p.SetValue(cpuInfo, item.Value);}}}return cpuInfo;}}/// <summary>/// CPU信息/// </summary>public class CPUInfo{/// <summary>/// 操作系统类型,32或64/// </summary>public uint AddressWidth { get; set; }/// <summary>/// 处理器的名称。如:Intel(R) Core(TM) i3-8100 CPU @ 3.60GHz/// </summary>public string Name { get; set; }/// <summary>/// 处理器的当前实例的数目。如:4。4核/// </summary>public uint NumberOfEnabledCore { get; set; }/// <summary>/// 用于处理器的当前实例逻辑处理器的数量。如:4。4线程/// </summary>public uint NumberOfLogicalProcessors { get; set; }/// <summary>/// 系统的名称。计算机名称。如:GREAMBWANG/// </summary>public string SystemName { get; set; }}#endregion#region 内存信息public partial class ComputerInfo{/// <summary>/// 内存信息/// </summary>/// <returns></returns>public static RAMInfo GetRAMInfo(){var ramInfo = new RAMInfo();var totalPhysicalMemory = TotalPhysicalMemory;var memoryAvailable = MemoryAvailable;var conversionValue = 1024.0 * 1024.0 * 1024.0;ramInfo.TotalPhysicalMemoryGBytes = Math.Round((double)(totalPhysicalMemory / conversionValue), 1);ramInfo.MemoryAvailableGBytes = Math.Round((double)(memoryAvailable / conversionValue), 1);ramInfo.UsedMemoryGBytes = Math.Round((double)((totalPhysicalMemory - memoryAvailable) / conversionValue), 1);ramInfo.UsedMemoryRatio = (int)(((totalPhysicalMemory - memoryAvailable) * 100.0) / totalPhysicalMemory);return ramInfo;}/// <summary>/// 总物理内存(B)/// </summary>/// <returns></returns>public static long TotalPhysicalMemory{get{long totalPhysicalMemory = 0;ManagementClass mc = new ManagementClass("Win32_ComputerSystem");ManagementObjectCollection moc = mc.GetInstances();foreach (ManagementObject mo in moc){if (mo["TotalPhysicalMemory"] != null){totalPhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());}}return totalPhysicalMemory;}}/// <summary>/// 获取可用内存(B)/// </summary>public static long MemoryAvailable{get{long availablebytes = 0;ManagementClass mos = new ManagementClass("Win32_OperatingSystem");foreach (ManagementObject mo in mos.GetInstances()){if (mo["FreePhysicalMemory"] != null){availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());}}return availablebytes;}}}/// <summary>/// 内存信息/// </summary>public class RAMInfo{/// <summary>/// 总物理内存(GB)/// </summary>public double TotalPhysicalMemoryGBytes { get; set; }/// <summary>/// 获取可用内存(GB)/// </summary>public double MemoryAvailableGBytes { get; set; }/// <summary>/// 获取已用内存(GB)/// </summary>public double UsedMemoryGBytes { get; set; }/// <summary>/// 内存使用率/// </summary>public int UsedMemoryRatio { get; set; }}#endregion#region 屏幕信息public partial class ComputerInfo{/// <summary>/// 屏幕信息/// </summary>public static GPUInfo GetGPUInfo(){GPUInfo gPUInfo = new GPUInfo();gPUInfo.CurrentResolution = MonitorHelper.GetResolution();gPUInfo.MaxScreenResolution = GetGPUInfo2();return gPUInfo;}/// <summary>/// 获取最大分辨率/// </summary>/// <returns></returns>private static Size GetMaximumScreenSizePrimary(){var scope = new System.Management.ManagementScope();var q = new System.Management.ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");UInt32 maxHResolution = 0;UInt32 maxVResolution = 0;using (var searcher = new System.Management.ManagementObjectSearcher(scope, q)){var results = searcher.Get();foreach (var item in results){if ((UInt32)item["HorizontalResolution"] > maxHResolution)maxHResolution = (UInt32)item["HorizontalResolution"];if ((UInt32)item["VerticalResolution"] > maxVResolution)maxVResolution = (UInt32)item["VerticalResolution"];}}return new Size((int)maxHResolution, (int)maxVResolution);}/// <summary>/// 获取最大分辨率2/// CurrentHorizontalResolution:1920/// CurrentVerticalResolution:1080/// </summary>/// <returns></returns>public static Size GetGPUInfo2(){var gpu = new StringBuilder();var moc = new ManagementObjectSearcher("select * from Win32_VideoController").Get();var currentHorizontalResolution = 0;var currentVerticalResolution = 0;foreach (var mo in moc){foreach (var item in mo.Properties){if (item.Name == "CurrentHorizontalResolution" && item.Value != null)currentHorizontalResolution = int.Parse((item.Value.ToString()));if (item.Name == "CurrentVerticalResolution" && item.Value != null)currentVerticalResolution = int.Parse((item.Value.ToString()));//gpu.Append($"{item.Name}:{item.Value}\r\n");}}//var res = gpu.ToString();//return res;return new Size(currentHorizontalResolution, currentVerticalResolution);}}public class MonitorHelper{/// <summary>/// 获取DC句柄/// </summary>[DllImport("user32.dll")]static extern IntPtr GetDC(IntPtr hdc);/// <summary>/// 释放DC句柄/// </summary>[DllImport("user32.dll", EntryPoint = "ReleaseDC")]static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hdc);/// <summary>/// 获取句柄指定的数据/// </summary>[DllImport("gdi32.dll")]static extern int GetDeviceCaps(IntPtr hdc, int nIndex);/// <summary>/// 获取设置的分辨率(修改缩放,该值不改变)/// {Width = 1920 Height = 1080}/// </summary>/// <returns></returns>public static Size GetResolution(){Size size = new Size();IntPtr hdc = GetDC(IntPtr.Zero);size.Width = GetDeviceCaps(hdc, DeviceCapsType.DESKTOPHORZRES);size.Height = GetDeviceCaps(hdc, DeviceCapsType.DESKTOPVERTRES);ReleaseDC(IntPtr.Zero, hdc);return size;}/// <summary>/// 获取屏幕物理尺寸(mm,mm)/// {Width = 476 Height = 268}/// </summary>/// <returns></returns>public static Size GetScreenSize(){Size size = new Size();IntPtr hdc = GetDC(IntPtr.Zero);size.Width = GetDeviceCaps(hdc, DeviceCapsType.HORZSIZE);size.Height = GetDeviceCaps(hdc, DeviceCapsType.VERTSIZE);ReleaseDC(IntPtr.Zero, hdc);return size;}/// <summary>/// 获取屏幕的尺寸---inch/// 21.5/// </summary>/// <returns></returns>public static float GetScreenInch(){Size size = GetScreenSize();double inch = Math.Round(Math.Sqrt(Math.Pow(size.Width, 2) + Math.Pow(size.Height, 2)) / 25.4, 1);return (float)inch;}}/// <summary>/// GetDeviceCaps 的 nidex值/// </summary>public class DeviceCapsType{public const int DRIVERVERSION = 0;public const int TECHNOLOGY = 2;public const int HORZSIZE = 4;//以毫米为单位的显示宽度public const int VERTSIZE = 6;//以毫米为单位的显示高度public const int HORZRES = 8;public const int VERTRES = 10;public const int BITSPIXEL = 12;public const int PLANES = 14;public const int NUMBRUSHES = 16;public const int NUMPENS = 18;public const int NUMMARKERS = 20;public const int NUMFONTS = 22;public const int NUMCOLORS = 24;public const int PDEVICESIZE = 26;public const int CURVECAPS = 28;public const int LINECAPS = 30;public const int POLYGONALCAPS = 32;public const int TEXTCAPS = 34;public const int CLIPCAPS = 36;public const int RASTERCAPS = 38;public const int ASPECTX = 40;public const int ASPECTY = 42;public const int ASPECTXY = 44;public const int SHADEBLENDCAPS = 45;public const int LOGPIXELSX = 88;//像素/逻辑英寸(水平)public const int LOGPIXELSY = 90; //像素/逻辑英寸(垂直)public const int SIZEPALETTE = 104;public const int NUMRESERVED = 106;public const int COLORRES = 108;public const int PHYSICALWIDTH = 110;public const int PHYSICALHEIGHT = 111;public const int PHYSICALOFFSETX = 112;public const int PHYSICALOFFSETY = 113;public const int SCALINGFACTORX = 114;public const int SCALINGFACTORY = 115;public const int VREFRESH = 116;public const int DESKTOPVERTRES = 117;//垂直分辨率public const int DESKTOPHORZRES = 118;//水平分辨率public const int BLTALIGNMENT = 119;}/// <summary>/// 屏幕信息/// </summary>public class GPUInfo{/// <summary>/// 当前分辨率/// </summary>public Size CurrentResolution { get; set; }/// <summary>/// 最大分辨率/// </summary>public Size MaxScreenResolution { get; set; }}#endregion#region 硬盘信息public partial class ComputerInfo{/// <summary>/// 磁盘信息/// </summary>public static List<DiskInfo> GetDiskInfo(){List<DiskInfo> diskInfos = new List<DiskInfo>();try{var moc = new ManagementClass("Win32_LogicalDisk").GetInstances();foreach (ManagementObject mo in moc){var diskInfo = new DiskInfo();var diskInfoType = diskInfo.GetType();var diskInfoFields = diskInfoType.GetProperties().ToList();foreach (var item in mo.Properties){if (diskInfoFields.Exists(f => f.Name == item.Name)){var p = diskInfoType.GetProperty(item.Name);p.SetValue(diskInfo, item.Value);}}diskInfos.Add(diskInfo);}//B转GBfor (int i = 0; i < diskInfos.Count; i++){diskInfos[i].Size = Math.Round((double)(diskInfos[i].Size / 1024.0 / 1024.0 / 1024.0), 1);diskInfos[i].FreeSpace = Math.Round((double)(diskInfos[i].FreeSpace / 1024.0 / 1024.0 / 1024.0), 1);}}catch (Exception ex){}return diskInfos;}}/// <summary>/// 磁盘信息/// </summary>public class DiskInfo{/// <summary>/// 磁盘ID 如:D:/// </summary>public string DeviceID { get; set; }/// <summary>/// 驱动器类型。3为本地固定磁盘,2为可移动磁盘/// </summary>public uint DriveType { get; set; }/// <summary>/// 磁盘名称。如:系统/// </summary>public string VolumeName { get; set; }/// <summary>/// 描述。如:本地固定磁盘/// </summary>public string Description { get; set; }/// <summary>/// 文件系统。如:NTFS/// </summary>public string FileSystem { get; set; }/// <summary>/// 磁盘容量(GB)/// </summary>public double Size { get; set; }/// <summary>/// 可用空间(GB)/// </summary>public double FreeSpace { get; set; }public override string ToString(){return $"{VolumeName}({DeviceID}), {FreeSpace}GB is available for {Size}GB in total, {FileSystem}, {Description}";}}#endregion

可以获取下面这些信息:

ComputerCheck Info:
System Info:Windows 10 Enterprise, Enterprise, X64, Microsoft Windows 10.0.18363 
CPU Info:Intel(R) Core(TM) i3-8100 CPU @ 3.60GHz, 4 Core 4 Threads
RAM Info:12/15.8GB(75%)
GPU Info:1920*1080 / 1920*1080
Disk Info:系统(C:), 74.2GB is available for 238.1GB in total, NTFS, 本地固定磁盘
软件(D:), 151.9GB is available for 300GB in total, NTFS, 本地固定磁盘
办公(E:), 30.7GB is available for 300GB in total, NTFS, 本地固定磁盘
其它(F:), 256.3GB is available for 331.5GB in total, NTFS, 本地固定磁盘

参考文章:

WMI_04_常见的WMI类的属性_wmi scsilogicalunit_fantongl的博客-CSDN博客

C#获取计算机物理内存和可用内存大小封装类SystemInfo_c# 获得物理内存大小_CodingPioneer的博客-CSDN博客

https://www.cnblogs.com/pilgrim/p/15115925.html

win10各种版本英文名称是什么? - 编程之家

https://www.cnblogs.com/dongweian/p/14182576.html

C# 使用WMI获取所有服务的信息_c# wmi_FireFrame的博客-CSDN博客

WMI_02_常用WMI查询列表_fantongl的博客-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/41026.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

flinksql报错 Cannot determine simple type name “org“

flink版本 1.15 报错内容 2023-08-17 15:46:02 java.lang.RuntimeException: Could not instantiate generated class WatermarkGenerator$0at org.apache.flink.table.runtime.generated.GeneratedClass.newInstance(GeneratedClass.java:74)at org.apache.flink.table.runt…

低功耗、5Mbps、RS-422 接口电路MS2583/MS2583M

MS2583/MS2583M 是一款低功耗、 5Mbps 、高 ESD 能力的 RS422 通讯接口电路。 在接收状态下&#xff0c;其功耗仅为 0.3mA 左右。 A/B 端 ESD 耐压可达 15kV &#xff0c;且无自激现象。当输出短路发生大电 流导致电路温度过高时&#xff0c;开启内部过温保护电路&…

go 使用 make 初始化 slice 切片使用注意

go 使用 make 初始化 slice 切片 时指定长度和不指定长度的情况 指定长度 package mainimport "fmt"func main() {s1 : make([]int, 5)data : []int{1, 2, 3}for _, v : range data {s1 append(s1, v)}fmt.Println(s1) }// 以上代码会输出 // [0 0 0 0 0 1 2 3] //…

vue中的路由缓存和解决方案

路由缓存的原因 解决方法 推荐方案二&#xff0c;使用钩子函数beforeRouteUpdate&#xff0c;每次路由更新前执行

手写spring笔记

手写spring笔记 《Spring 手撸专栏》笔记 IoC部分 Bean初始化和属性注入 Bean的信息封装在BeanDefinition中 /*** 用于记录Bean的相关信息*/ public class BeanDefinition {/*** Bean对象的类型*/private Class beanClass;/*** Bean对象中的属性信息*/private PropertyVal…

MFC第三十天 通过CToolBar类开发文字工具栏和工具箱、GDI+边框填充以及基本图形的绘制方法、图形绘制过程的反色线模型和实色模型

文章目录 CControlBar通过CToolBar类开发文字工具栏和工具箱CMainFrame.hCAppCMainFrm.cppCMainView.hCMainView.cppCEllipse.hCEllipse.cppCLine.hCLine.cppCRRect .hCRRect .cpp CControlBar class AFX_NOVTABLE CControlBar : public CWnd{DECLARE_DYNAMIC(CControlBar)pro…

OC调用Swift编写的framework

一、前言 随着swift趋向稳定&#xff0c;越来越多的公司都开始用swift来编写苹果相关的业务了&#xff0c;关于swift的利弊这里就不多说了。这里详细介绍OC调用swift编写的framework库的步骤 二、制作framework 1、新建项目&#xff0c;选择framework 2、填写framework的名称…

AutoHotkey:定时删除目录下指定分钟以前的文件,带UI界面

删除指定目录下&#xff0c;所有在某个指定分钟以前的文件&#xff0c;可以用来清理经常生成很多文件的目录&#xff0c;但又需要保留最新的一部分文件 支持拖放目录到界面 能够记忆设置&#xff0c;下次启动后不用重新设置&#xff0c;可以直接开始 应用场景比如&#xff1a…

WinForm内嵌Unity3D

Unity3D可以C#脚本进行开&#xff0c;使用vstu2013.msi插件&#xff0c;可以实现在VS2013中的调试。在开发完成后&#xff0c;由于项目需要&#xff0c;需要将Unity3D嵌入到WinForm中。WinForm中的UnityWebPlayer Control可以载入Unity3D。先看效果图。 一、为了能够动态设置ax…

【boost网络库从青铜到王者】第五篇:asio网络编程中的同步读写的客户端和服务器示例

文章目录 1、简介2、客户端设计3、服务器设计3.1、session函数3.2、StartListen函数3、总体设计 4、效果测试5、遇到的问题5.1、服务器遇到的问题5.1.1、不用显示调用bind绑定和listen监听函数5.1.2、出现 Error occured!Error code : 10009 .Message: 提供的文件句柄无效。 [s…

Failed to execute goal org.apache.maven.plugins

原因&#xff1a; 这个文件D:\java\maven\com\ruoyi\pg-student\maven-metadata-local.xml出了问题 解决&#xff1a; 最简单的直接删除D:\java\maven\com\ruoyi\pg-student\maven-metadata-local.xml重新打包 或者把D:\java\maven\com\ruoyi\pg-student这个目录下所有文件…

性能测试场景设计

性能测试场景设计&#xff0c;是性能测试中的重要概念&#xff0c;性能测试场景设计&#xff0c;目的是要描述如何执行性能测试。 通常来讲&#xff0c;性能测试场景设计主要会涉及以下部分&#xff1a; 并发用户数是多少&#xff1f; 测试刚开始时&#xff0c;以什么样的速率…

Spring WebFlux 的详细介绍

Spring WebFlux 是基于响应式编程的框架&#xff0c;用于构建异步、非阻塞的 Web 应用程序。它是Spring框架的一部分&#xff0c;专注于支持响应式编程范式&#xff0c;使应用程序能够高效地处理大量的并发请求和事件。 以下是关于 Spring WebFlux 的详细介绍&#xff1a; 1. *…

go入门实践五-实现一个https服务

文章目录 前言生成证书申请免费的证书使用Go语言生成自签CA证书 https的客户端和服务端服务端代码客户端代码 tls的客户端和服务端服务端客户端 前言 在公网中&#xff0c;我想加密传输的数据。(1)很自然&#xff0c;我想到了把数据放到http的请求中&#xff0c;然后通过tls确…

2023年京东宠物食品行业数据分析(京东大数据)

宠物食品市场需求主要来自于养宠规模&#xff0c;近年来由于我国宠物数量及养宠人群的规模均在不断扩大&#xff0c;宠物相关产业和市场规模也在蓬勃发展&#xff0c;宠物食品市场也同样保持正向增长。 根据鲸参谋电商数据分析平台的相关数据显示&#xff0c;2023年1月-7月&am…

vue5种模糊查询方式

在Vue中&#xff0c;有多种方式可以实现模糊查询。以下是五种常见的模糊查询方式&#xff1a; 使用JavaScript的filter()方法&#xff1a;使用filter()方法可以对数组进行筛选&#xff0c;根据指定的条件进行模糊查询。例如&#xff1a; data() {return {items: [{ name: App…

接口自动化测试(添加课程接口调试,调试合同上传接口,合同列表查询接口,批量执行)

1、我们把信息截取一下 1.1 添加一个新的请求 1.2 对整个请求进行保存&#xff0c;Ctrl S 2、这一次我们添加的是课程添加接口&#xff0c;以后一个接口完成&#xff0c;之后Ctrl S 就能够保存 2.1 选择方法 2.2 设置请求头&#xff0c;参数数据后期我们通过配置设置就行 3、…

收银一体化-亿发2023智慧门店新零售营销策略,实现全渠道运营

伴随着互联网电商行业的兴起&#xff0c;以及用户理念的改变&#xff0c;大量用户从线下涌入线上&#xff0c;传统的线下门店人流量急剧收缩&#xff0c;门店升级几乎成为了每一个零售企业的发展之路。智慧门店新零售收银解决方案是针对传统零售企业面临的诸多挑战和问题&#…

Mathematica 与 Matlab 常见复杂指令集汇编

Mathematica 常见指令汇编 Mathematica 常见指令 NDSolve 求解结果的保存 sol NDSolve[{y[x] x^2, y[0] 0, g[x] -y[x]^2, g[0] 1}, {y, g}, {x, 0, 1}]; numericSoly sol[[1, 1, 2]]; numericSolg sol[[1, 2, 2]]; data Table[{x, numericSoly[x], numericSolg[x]},…

JVM——类加载器

回顾一下类加载过程 类加载过程&#xff1a;加载->连接->初始化。连接过程又可分为三步:验证->准备->解析。 一个非数组类的加载阶段&#xff08;加载阶段获取类的二进制字节流的动作&#xff09;是可控性最强的阶段&#xff0c;这一步我们可以去完成还可以自定义…