日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
c#獲取屬性名稱
在C#中,可以使用反射獲取屬性名稱。

C如何獲取電腦的相關(guān)屬性

創(chuàng)新互聯(lián)公司是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、電商網(wǎng)站制作開發(fā)、重慶小程序開發(fā)公司、微信營銷、系統(tǒng)平臺開發(fā),與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!

在編程過程中,我們經(jīng)常需要獲取電腦的相關(guān)信息,例如操作系統(tǒng)版本、CPU信息、內(nèi)存大小等,本文將介紹如何在C中獲取這些屬性。

獲取操作系統(tǒng)版本

要獲取操作系統(tǒng)版本,可以使用System.Environment類的OSVersion屬性,以下是一個簡單的示例:

using System;
class Program
{
    static void Main()
    {
        var osVersion = Environment.OSVersion;
        Console.WriteLine("操作系統(tǒng)版本:");
        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類,以下是一個簡單的示例:

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($"核心數(shù): {cpuInfo["NumberOfCores"]}");
        Console.WriteLine($"線程數(shù): {cpuInfo["NumberOfLogicalProcessors"]}");
    }
    private static Dictionary GetCpuInfo()
    {
        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信息即可,后續(xù)可以添加其他屬性獲取
        }
        return cpuInfo;
    }
}

獲取內(nèi)存大小(以字節(jié)為單位)

要獲取內(nèi)存大小,可以使用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 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(); if (systemMemoryStatusEx != null) memorySize += systemMemoryStatusEx.WorkingSetSize; if (shellMemoryCounters != null) memorySize += shellMemoryCounters.PrivateUsage * Marshal.SizeOf(); return memorySize; catch (Exception ex) { Console.WriteLine($"無法獲取內(nèi)存大小{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。

網(wǎng)站標(biāo)題:c#獲取屬性名稱
文章源于:http://www.dlmjj.cn/article/dpjhpop.html