av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

c#獲取屬性名稱

在C#中,可以使用反射獲取屬性名稱。

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

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

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

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

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

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類(lèi),以下是一個(gè)簡(jiǎn)單的示例:

using System;
using System.Management;
class Program
{
    static void Main()
    {
        var cpuInfo = GetCpuInfo();
        Console.WriteLine("CPU信息:");
        Console.WriteLine($"制造商: {cpuInfo["Manufacturer"]}");
        Console.WriteLine($"型號(hào): {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; // 只獲取第一個(gè)CPU信息即可,后續(xù)可以添加其他屬性獲取
        }
        return cpuInfo;
    }
}

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

要獲取內(nèi)存大小,可以使用System.Management命名空間下的ManagementObjectSearcher類(lèi),以下是一個(gè)簡(jiǎn)單的示例:

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,或者手動(dòng)添加引用DllImport("shell32.dll") public static class SHELL32; // 需要安裝NuGet包Microsoft.WindowsAPICodePack-Shell,或者手動(dòng)添加引用DllImport("kernel32.dll") public static class GetSystemInfoEx; // 需要安裝NuGet包Microsoft.WindowsAPICodePack-Shell,或者手動(dòng)添加引用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($"無(wú)法獲取內(nèi)存大小{ex}"); return default; } // 需要安裝NuGet包Microsoft.WindowsAPICodePack-Shell,或者手動(dòng)添加引用[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,或者手動(dòng)添加引用[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,或者手動(dòng)添加引用[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,或者手動(dòng)添加引用[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,或者手動(dòng)添加引用[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,或者手動(dòng)添加引用[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,或者手動(dòng)寫(xiě)代碼直接使用[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,或者手動(dòng)寫(xiě)代碼直接使用[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,或者手動(dòng)寫(xiě)代碼直接使用[Out][StructLayout(LayoutKind。

新聞標(biāo)題:c#獲取屬性名稱
本文網(wǎng)址:http://uogjgqi.cn/article/dpjhpop.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流