Reading the registry...

Hi there,

I know how to get gernal information out of the registry, but I'm having trouble getting the main-memory.
It's located in:
HKEY_LOCAL_MACHINE\HARDWARE\RESOURCEMAP\System Resources\Physical Memory
The field name is:
.Translated
The field type is:
REG_RESOURCE_LIST

I tried:

RegistryKey rk = Registry.LocalMachine;

rk = rk.OpenSubKey("HARDWARE\RESOURCEMAP\System Resources\Physical Memory", false);

Console.WriteLine((rk.GetValue(".Translated")).ToString());


But this doesn't work, as I get the following exception:
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
I think it's because of the field type...

Can sombody help me with this

Thanks,
Finch.


Answer this question

Reading the registry...

  • Karamasov

    You should actually use the System.Management classes to retrieve this kind of information. Almost any information about the system hardware can be retrieved in this way. Here's some code from EggheadCafe.com that might describe what you want to do:



    using System.Management;

    ManagementScope oMs = new ManagementScope();
    ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
    ManagementObjectCollection oCollection = oSearcher.Get();

    int MemSize = 0;
    int mCap = 0;

    foreach (ManagementObject obj in oCollection)
    {
        mCap = Convert.ToInt32(obj["Capacity"]);
        MemSize += mCap;
    }
    MemSize = (MemSize / 1024) / 1024;

     


  • Reading the registry...