user3375027
user3375027

Reputation: 87

Registry subkey invisible?? C#

So I am attempting to implement a registry system into a program I am building. So far, the main of the program has no problems opening, editing, or deleting any of the values associated with this key.

However, my cleaning program does, and when I tell it to show me all subkeys using:

Registry.LocalMachine.OpenSubKey (regappend + reg, true).GetSubKeyNames ();

It tells me the value does not exist, or is null, and so I cannot grab information from it as a result.

private static readonly string regappend = "SOFTWARE\\";
private static string reg = "EL\\Main";

// Malfunctioning code:

using (RegistryKey myKey = Registry.LocalMachine.OpenSubKey (regappend + reg, true)) 
{
    if (myKey != null) 
    {
        foreach (string s in myKey.GetValueNames()) 
        {
            Console.WriteLine (s);
        }

        Console.WriteLine (myKey + "\n" + myKey.GetValue ("F") + "\n");
    }
}

// Working Code:

using (RegistryKey myKey = Registry.LocalMachine.OpenSubKey (regappend + reg, true)) 
{
    if (myKey.GetValue ("F") != null) 
    {
        f = (string)myKey.GetValue ("F");

        if (debugmode == true) 
        {
            Console.WriteLine (f);
        }
    }
}

Variables are the same throughout.

What am I doing wrong?

Upvotes: 0

Views: 627

Answers (1)

Rufus L
Rufus L

Reputation: 37020

Your program ran fine for me after I created the key using this code:

using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true))
{
    key.CreateSubKey("El\\Main").SetValue("F", "bar");
}

Note that, when viewing in Regedit, the key is actually located (on my machine) under:

HKLM\Software\Wow6432Node\El\Main

Possibly the reason you're not getting the value you expect is that you're looking in the wrong location in the registry.

Upvotes: 1

Related Questions