c# 获取属性名称

在C#中,可以使用反射获取属性名称。

C如何获取电脑的相关属性

在编程过程中,我们经常需要获取电脑的相关信息,例如操作系统版本、CPU信息、内存大小等,本文将介绍如何在C中获取这些属性。

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信息

要获取CPU信息,可以使用System.Management命名空间下的ManagementObjectSearcher类,以下是一个简单的示例:

c# 获取属性名称

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 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信息即可,后续可以添加其他属性获取
        }
        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 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,或者手动添加引用[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。

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/131687.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
酷盾叔
上一篇 2024-01-02 05:37
下一篇 2024-01-02 05:41

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入