
地 址:上海市长宁66号
电 话:18006757605
网址:bjasbd.com
邮 箱:78158011@qq.com
C如何获取电脑的取属相关属性
在编程(cheng)过程(cheng)中,我(wo)们经常需要获取电脑的性名相(xiang)关信息,例如(ru)操作系统版本、取属CPU信息、性(xing)名内存大(da)小等,取属本文将介绍如何在C中获取这些属性。性名


要获取操作系统版本,取属可以使用System.Environment类的(de)性名OSVersion属性,以下是取属一个简单的示例:

using System;class Program{ static void Main() { var osVersion = Environment.OSVersion; Console.WriteLine("操作(zuo)系统版本:"); 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类(lei),以下是取属一个简单的示例:
using System;using System.Management;class Program{ static void Main() { var cpuInfo = GetCpuInfo(); Console.WriteLine("CPU信息:"); Console.WriteLine($"制(zhi)造商: { cpuInfo["Manufacturer"]}"); Console.WriteLine($"型(xing)号(hao): { cpuInfo["Name"]}"); Console.WriteLine($"核心数: { cpuInfo["NumberOfCores"]}"); Console.WriteLine($"线程数: { cpuInfo["NumberOfLogicalProcessors"]}"); } private static Dictionary<string, object> GetCpuInfo() { var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); var collection = searcher.Get(); var cpuInfo = new Dictionary<string, object>(); foreach (var item in collection) { cpuInfo["Manufacturer"] = item["Manufacturer"]; cpuInfo["Name"] = item["Name"]; cpuInfo["NumberOfCores"] = item["NumberOfCores"]; cpuInfo["NumberOfLogicalProcessors"] = item["NumberOfLogicalProcessors"]; break; // 只获取第一个CPU信息即可,后续可(ke)以添加其他属性获取 } return cpuInfo; }}要获取内存(cun)大小,性名可以使(shi)用(yong)System.Management命名空间下的(de)取属ManagementObjectSearcher类(lei),以下(xia)是一个简单的示(shi)例:
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 Task<ulong> GetMemorySizeAsync() => 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<IntPtr>(); if (systemMemoryStatusEx != null) memorySize += systemMemoryStatusEx.WorkingSetSize; if (shellMemoryCounters != null) memorySize += shellMemoryCounters.PrivateUsage * Marshal.SizeOf<IntPtr>(); 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,或者(zhe)手动添加(jia)引用(yong)[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,或(huo)者手动添加引用[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,或者(zhe)手动写代码直(zhi)接(jie)使用[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 // 需要安装(zhuang)NuGet包Microsoft.WindowsAPICodePack-Shell,或者手动(dong)写代码直接使用[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。