简体   繁体   English

如何获取已安装的 RAM 和 CPU 详细信息 c#.Net Core?

[英]How I can get the installed RAM and CPU details c# .Net Core?

I need a installed total RAM in GB and also the CPU details as well.我需要以 GB 为单位的已安装总 RAM 以及 CPU 详细信息。 I see lot post related to this.我看到很多与此相关的帖子。 But, I'm not sure which is suitable for me.但是,我不确定哪个适合我。 And, also I tried with couple of examples but its not provided the expected result.而且,我也尝试了几个例子,但它没有提供预期的结果。

For example, I need like below,例如,我需要如下所示,

RAM Size = 16 GB RAM 大小 = 16 GB

CPU = Intel(R) Core(TM) i5 3.20 GHz 2 Cores CPU = Intel(R) Core(TM) i5 3.20 GHz 2 核

Note: I'm not expect the available or used memory.注意:我不期望可用或使用的 memory。

Update:更新:

To get the RAM details I used the below code要获取 RAM 详细信息,我使用了以下代码

string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
        ManagementObjectSearcher searcher123 = new ManagementObjectSearcher(Query);
        foreach (ManagementObject WniPART in searcher123.Get())
        {
            UInt32 SizeinKB = Convert.ToUInt32(WniPART.Properties["MaxCapacity"].Value);
            UInt32 SizeinMB = SizeinKB / 1024;
            UInt32 SizeinGB = SizeinKB / (1024 * 1024);
            Console.WriteLine("Size in KB: {0}, Size in MB: {1}, Size in GB: {2}", SizeinKB, SizeinMB, SizeinGB);
        }

But, This gave me wrong Data(Infact, it give 32 GB instead of 16GB)但是,这给了我错误的数据(事实上,它给了 32 GB 而不是 16GB)

并且,要获取 CPU 详细信息

Looks like you're using the wrong class.看起来您使用了错误的 class。 Try the Win32_PhysicalMemory class and the Capacity property尝试Win32_PhysicalMemory class 和Capacity属性

var query = "SELECT Capacity FROM Win32_PhysicalMemory";
var searcher = new ManagementObjectSearcher(query);
foreach (var WniPART in searcher.Get())
{
    var capacity = Convert.ToUInt64(WniPART.Properties["Capacity"].Value);
    var capacityKB = capacity / 1024;
    var capacityMB = capacityKB / 1024;
    var capacityGB = capacityMB / 1024;
    System.Console.WriteLine("Size in KB: {0}, Size in MB: {1}, Size in GB: {2}", capacityKB, capacityMB, capacityGB);
}

The MaxCapacity property in the Win32_PhysicalMemoryArray class will give you the maximum amount of RAM installable on your machine, not the amount of memory that is actually installed. MaxCapacity中的Win32_PhysicalMemoryArray属性将为您提供机器上可安装的最大 RAM 量,而不是实际安装的 memory 量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 .NET CORE 在 C# web 应用程序中获取当前的 CPU/RAM/磁盘使用情况? - How to get the current CPU/RAM/Disk usage in a C# web application using .NET CORE? 如何在 C# 中获得每个内核的 CPU 负载? - How can I get CPU load per core in C#? 如何在不循环所有进程的情况下获取当前的 CPU 和 RAM 使用情况,以及 C# .NET 核心中有多少可用? - How to get current CPU and RAM usage, and also how much is available in C# .NET core without looping over all processes? 如何从c#应用程序获得远程linux机器的cpu和ram使用 - How can I get cpu and ram usage of remote linux machine from c# application 如何在C#(托管代码)中获得* THREAD *的CPU使用率和/或RAM使用率? - How can I get CPU usage and/or RAM usage of a *THREAD* in C# (managed code)? 如何在C#中获得CPU速度和总物理内存? - How do I get the CPU speed and total physical ram in C#? 如何将 CPU 内核限制为 C# 中的特定线程 - How Can I Limit CPU core to specific thread in C# 如何在 .NET Core 中获取 CPU 名称? - How can I get CPU name in .NET Core? 如何在C#中获取主板和CPU电压信息? - How can I get motherboard and CPU Voltage informations in C#? 如何在 Asp.Net Core 中使用 AmazonEC2Client 获取 ram 和 cpu 信息? - How to get ram and cpu info using AmazonEC2Client in Asp.Net Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM