C如何获取电脑的相关属性
创新互联公司是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括网站设计、网站建设、电商网站制作开发、重庆小程序开发公司、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!
在编程过程中,我们经常需要获取电脑的相关信息,例如操作系统版本、CPU信息、内存大小等,本文将介绍如何在C中获取这些属性。
要获取操作系统版本,可以使用System.Environment
类的OSVersion
属性,以下是一个简单的示例:
using System; class Program { static void Main() { var osVersion = Environment.OSVersion; Console.WriteLine("操作系统版本:"); Console.WriteLine($"Major Version: {osVersion.MajorVersion}"); Console.WriteLine($"Minor Version: {osVersion.MinorVersion}"); Console.WriteLine($"Build Number: {osVersion.BuildNumber}"); Console.WriteLine($"Revision: {osVersion.Revision}"); } }
要获取CPU信息,可以使用System.Management
命名空间下的ManagementObjectSearcher
类,以下是一个简单的示例:
using System; using System.Management; class Program { static void Main() { var cpuInfo = GetCpuInfo(); Console.WriteLine("CPU信息:"); Console.WriteLine($"制造商: {cpuInfo["Manufacturer"]}"); Console.WriteLine($"型号: {cpuInfo["Name"]}"); Console.WriteLine($"核心数: {cpuInfo["NumberOfCores"]}"); Console.WriteLine($"线程数: {cpuInfo["NumberOfLogicalProcessors"]}"); } private static DictionaryGetCpuInfo() { var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); var collection = searcher.Get(); var cpuInfo = new Dictionary (); foreach (var item in collection) { cpuInfo["Manufacturer"] = item["Manufacturer"]; cpuInfo["Name"] = item["Name"]; cpuInfo["NumberOfCores"] = item["NumberOfCores"]; cpuInfo["NumberOfLogicalProcessors"] = item["NumberOfLogicalProcessors"]; break; // 只获取第一个CPU信息即可,后续可以添加其他属性获取 } return cpuInfo; } }
要获取内存大小,可以使用System.Management
命名空间下的ManagementObjectSearcher
类,以下是一个简单的示例:
using System; using System.Management; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Microsoft.Win32; // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用DllImport("shell32.dll") public static class SHELL32; // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用DllImport("kernel32.dll") public static class GetSystemInfoEx; // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用DllImport("psapi", CharSet = CharSet.Auto)] public static class Psapi; [StructLayout(LayoutKind.Sequential)] public struct MEMORYSTATUSEX; public enum MemoryType { Private = 0, Shared = NonPaged, Total = NonPaged + Shared }; public struct PROCESS_MEMORY_COUNTERS_EX; public struct SHELL_MEMORY_COUNTERS; public static void Main() => Task.Run(async () => await GetMemorySizeAsync()).Wait(); async TaskGetMemorySizeAsync() => ulong memorySize = default; try // Windows Vista and later using SHELL32::SHGetMemInfo(out SHELL_MEMORY_COUNTERS shellMemoryCounters); // Windows XP using GetSystemInfoEx::GetSystemInfoEx(out MEMORYSTATUSEX memoryStatusEx); using (var process = Process.GetCurrentProcess()) using (var psutilProcessMemoryCounters = process?.SafeMemoryUsage()) using (var systemMemoryStatusEx = new MEMORYSTATUSEX()) // Windows Vista and later systemMemoryStatusEx = new MEMORYSTATUSEX(); // Windows XP systemMemoryStatusEx = GetSystemInfoEx(); if (psutilProcessMemoryCounters != null) memorySize += psutilProcessMemoryCounters.PrivateUsage Marshal.SizeOf (); if (systemMemoryStatusEx != null) memorySize += systemMemoryStatusEx.WorkingSetSize; if (shellMemoryCounters != null) memorySize += shellMemoryCounters.PrivateUsage * Marshal.SizeOf (); return memorySize; catch (Exception ex) { Console.WriteLine($"无法获取内存大小{ex}"); return default; } // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用[DllImport("psapi", CharSet = CharSet.Auto)] public static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS_EX pmc, int cb); [StructLayout(LayoutKind.Sequential)] public struct PROCESS_MEMORY_COUNTERS_EX {}; [StructLayout(LayoutKind.Sequential)] public struct SHELL_MEMORY_COUNTERS; [StructLayout(LayoutKind.Sequential)] public struct PSAPI_WORKING_SET_EX {}; // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用[DllImport("psapi", CharSet = CharSet.Auto)] public static extern bool GetProcessMemoryInfo(IntPtr hProcess, out SHELL_MEMORY_COUNTERS memCounters, int size); [DllImport("kernel32")] public static extern UInt64 GlobalMemoryStatusEx([Out] out MEMORYSTATUSEX lpBuffer); [StructLayout(LayoutKind.Sequential)] public struct MEMORYSTATUSEX // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用[DllImport("kernel32")] public static extern UInt64 GlobalMemoryStatusEx([Out] out MEMORYSTATUSEX[] buffer, UInt32 length); [StructLayout(LayoutKind.Sequential)] public struct SHELL_MEMORY_COUNTERS // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用[DllImport("psapi", CharSet = CharSet.Auto)] public static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PSAPI_WORKING_SET_EX psinfo, int size); [StructLayout(LayoutKind.Sequential)] public struct PSAPI_WORKING_SET_EX // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用[DllImport("psapi", CharSet = CharSet.Auto)] public static extern bool GetProcessMemoryInfo(IntPtr hProcess, out SHELL_MEMORY_COUNTERS shmemCounters, int size); [StructLayout(LayoutKind.Sequential)] public struct SHELL_MEMORY_COUNTERS // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动添加引用[DllImport("psapi", CharSet = CharSet.Auto)] public static extern bool GetProcessMemoryInfo(IntPtr hProcess, out SHELL_MEMORY_COUNTERS shmemCounters); [StructLayout(LayoutKind.Sequential)] public struct SHELL_MEMORY_COUNTERS // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动写代码直接使用[DllImport("kernel32")] public static extern UInt64 GlobalMemoryStatusEx([Out] out long lTotalPhys, [Out] out long lAvailPhys, [Out] out long lTotalPageFile, [Out] out long lAvailPageFile); [StructLayout(LayoutKind.Sequential)] public struct SHELL_MEMORY_COUNTERS // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动写代码直接使用[DllImport("kernel32")] public static extern UInt64 GlobalMemoryStatusEx2([Out] out long lTotalPhys, [Out] out long lAvailPhys, [Out] out long lTotalPageFile, [Out] out long lAvailPageFile); [StructLayout(LayoutKind.Sequential)] public struct SHELL_MEMORY_COUNTERS // 需要安装NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动写代码直接使用[Out][StructLayout(LayoutKind。
文章名称:c#获取属性名称
网站网址:http://www.shufengxianlan.com/qtweb/news14/228514.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联