简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM