获取已安装的内存类型

4

我试图获取安装在电脑上的RAM类型。我找到了一个代码示例,但它并不真正起作用,对于DDR2它总是显示未知。对于DDR3它也总是返回0...对于SDRAM来说可以正常工作。

编辑:应该注意它应该适用于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";
        }

    }

在我的笔记本电脑上运行良好。 - Jake1164
2个回答

1

.NET没有提供获取我所知道的内存类型的方法。

.Net中现有的方法只返回一个等效值,但是必须按照此指南手动将其转换为字符串:Win32_PhysicalMemory类

我已经为此目的制作了一个专门的类,在该类中,我将代码翻译为它们各自的名称。

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;
}

enter image description here


首先,为什么你将 Ram Type 声明为属性?其次,为什么你创建了另一个函数,而不是将它们合并为一个函数?你的代码很棒,但还是想问一下 :) - Aousaf Rashid

0

这个值和一些操作系统存在一些已知的问题。似乎它取决于内存本身,而不是所有内存都在其EEPROM中编码了信息。

一个替代方案可能是直接查询SMBIOS。

此外,这里有一个程序(虽然是用c++编写的),你可以用它来诊断你的问题,并获得获取所需信息的想法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接