c# - 获取安装内存类型

标签 c# ram

我正在尝试获取 PC 上安装的 RAM 类型。我能够找到一个代码示例,但它并没有真正起作用,它总是向我显示 Unknown for 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";
        }

    }

最佳答案

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

.Net 中的现有方法仅返回等效值,但必须按照以下指南手动完成到字符串的转换:Win32_PhysicalMemory class

我为此专门制作了一个类,我将代码翻译成它们各自的名称

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

关于c# - 获取安装内存类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17341468/

相关文章:

c# - 使用新的 Azure.messaging.servicebus 获取事件消息计数

c# - DISTINCT() 和 ORDERBY 问题

c - #include 会增加 RAM 使用吗?

c++ - 无法使用 GetProcessMemoryInfo 测量静态数组内存使用情况

c# - 中继器内的语音气泡

c# - 如何使用 MySQL 配置流畅的 nHibernate

c# - SSIS 脚本不断恢复到 .Net Framework 4.5

c# - 获取进程 RAM 使用情况(以兆字节为单位)

c - 是否存在常量数据应加载到 RAM 而不是直接闪存访问的情况

c - 处理器如何读取内存?