Search code examples
c#registry

Read the data from a Key in Registry and parse it


I created a folder under

{HKEY_LOCAL_MACHINE\Software\MyKeys}

MyKeys folder contains two SubKeys

MyKeys==>MF This folder contains a string Weekday with Data Mon,Tue,Wed,Thu,Fri,Sat,Sun

MyKeys==>TF This folder contains a string Weekday with data Tue,Wed,Thu,Fri,Sat

What I'm trying to accomplish is to read the data from the first key and parse it and store it in a variable so I can later use it in my C# console application to see if the current day is in one of the items in that registry.

Here's how I try to get to that location.

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\MyKeys");

However, registryKey is null. But if I change the location

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\Intel");

It will read that location.

Any suggestions on how I can accomplish this? Is there a special permission required for that location to be accessed from applications?

EDIT: Here's an image to make it a little clear.

enter image description here


Solution

  • try:

    RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32).OpenSubKey("Software\\MyKeys");
    
    string filePath = (string)rk.GetValue("Weekday");
    

    Registry keys get stored in a different location on a 64 bit machine. although they are visible in that location if you use Regedit they are really located in a different location

    EDIT

    On 64-bit Windows, portions of the registry entries are stored separately for 32-bit application and 64-bit applications and mapped into separate logical registry views using the registry redirector and registry reflection, because the 64-bit version of an application may use different registry keys and values than the 32-bit version. There are also shared registry keys that are not redirected or reflected.

    32-bit and 64-bit Application Data in the Registry