简体   繁体   English

获取已安装的RAM类型

[英]Get Installed RAM Type

I am trying to get the type of RAM thats installed on the PC. 我试图获得PC上安装的RAM类型。 I was able to find a code example but it does not really work, it shows me always Unknown for DDR2. 我能够找到一个代码示例,但它并没有真正起作用,它告诉我DDR2总是未知。 It also always returns 0 for DDR3.. words fine for SDRAM 对于DDR3,它也总是返回0.对于SDRAM来说这个字很好

edit: should not that its suppose to work on XP. 编辑:不应该认为它可以在XP上运行。

    public string RAM_Type()
    {

        int type=0;
        var searcher = new ManagementObjectSearcher("Select * from Win32_PhysicalMemory");
        foreach (ManagementObject obj in searcher.Get())
        {
           type = Int32.Parse(obj.GetPropertyValue("MemoryType").ToString());

        }

        switch (type)
        {
            case 20:
                return "DDR";
                break;
            case 21:
                return "DDR-2";
                break;
            case 17:
                return "SDRAM";
                break;
            default:
                if (type == 0 || type > 22)
                    return "DDR-3";
                else
                    return "Unknown";
        }

    }

.NET does not provide a way to get the type of memory I know. .NET没有提供一种获取我所知道的内存类型的方法。

The existing methodology in .Net only returns an equivalent value, but the conversion to string must be done manually following this guide: Win32_PhysicalMemory class .Net中的现有方法仅返回等效值,但必须按照本指南手动完成到字符串的转换: Win32_PhysicalMemory类

I have made a specialized class for this purpose, where I translate the codes to their respective names 我已经为此目的创建了一个专门的类,我将代码翻译成各自的名称

using System;
using System.Management;

namespace Hector
{
    public class RamInfo
    {
        public static string RamType
        {
            get
            {
                int type = 0;

                ConnectionOptions connection = new ConnectionOptions();
                connection.Impersonation = ImpersonationLevel.Impersonate;
                ManagementScope scope = new ManagementScope("\\\\.\\root\\CIMV2", connection);
                scope.Connect();
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PhysicalMemory");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    type = Convert.ToInt32(queryObj["MemoryType"]);
                }

                return TypeString(type);
            }
        }

        private static string TypeString(int type)
        {
            string outValue = string.Empty;

            switch (type)
            {
                case 0x0: outValue = "Unknown"; break;
                case 0x1: outValue = "Other"; break;
                case 0x2: outValue = "DRAM"; break;
                case 0x3: outValue = "Synchronous DRAM"; break;
                case 0x4: outValue = "Cache DRAM"; break;
                case 0x5: outValue = "EDO"; break;
                case 0x6: outValue = "EDRAM"; break;
                case 0x7: outValue = "VRAM"; break;
                case 0x8: outValue = "SRAM"; break;
                case 0x9: outValue = "RAM"; break;
                case 0xa: outValue = "ROM"; break;
                case 0xb: outValue = "Flash"; break;
                case 0xc: outValue = "EEPROM"; break;
                case 0xd: outValue = "FEPROM"; break;
                case 0xe: outValue = "EPROM"; break;
                case 0xf: outValue = "CDRAM"; break;
                case 0x10: outValue = "3DRAM"; break;
                case 0x11: outValue = "SDRAM"; break;
                case 0x12: outValue = "SGRAM"; break;
                case 0x13: outValue = "RDRAM"; break;
                case 0x14: outValue = "DDR"; break;
                case 0x15: outValue = "DDR2"; break;
                case 0x16: outValue = "DDR2 FB-DIMM"; break;
                case 0x17: outValue = "Undefined 23"; break;
                case 0x18: outValue = "DDR3"; break;
                case 0x19: outValue = "FBD2"; break;
                case 0x1a: outValue = "DDR4"; break;
                default: outValue = "Undefined"; break;
            }

            return outValue;
        }
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    label1.Text = Hector.RamInfo.RamType;
}

在此输入图像描述

There are some known issues with this value and some OS's. 这个值和一些操作系统存在一些已知问题 It also would appear that it depends on the memory itself and not all memory has the information encoded in its EEPROM. 它似乎也取决于存储器本身,并非所有存储器都具有在其EEPROM中编码的信息。

One alternative could be to query the SMBIOS directly. 一种替代方法是直接查询SMBIOS。

Also here is a program (although in c++) that you might be able to help diagnose your issues as well as get ideas of how to get at the information you are looking to retrieve. 此外,这是一个程序 (尽管在c ++中),您可能能够帮助诊断您的问题,并获得如何获取您想要检索的信息的想法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM