Question

I'd like to preface this by highlighting that I am fairly new to C#.

I'm trying to make a program to find and edit a registry value in order to disable CPU core parking using this method: Registry Edit

The issue is that I know the start of the key:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\

But not the Next part but I know the segment after that:

0cc5b647-c1df-4637-891a-dec35c318583

So if it looks like this:

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\PowerSettings\<UNKNOWN>\0cc5b647-c1df-4637-891a-dec35c318583

How do I find the using Registry and Registrykey? I tried looping through all subkeys and I just get an exception because all I get back is null.

Any suggestions appreciated.

Was it helpful?

Solution

You can use recursion for finding SubKey.

private RegistryKey SearchSubKey(RegistryKey Key,String KeyName)
{
    foreach (String subKey in Key.GetSubKeyNames())
    {
        RegistryKey key1 = Key.OpenSubKey( subKey);

        if (subKey.ToUpper() == KeyName.ToUpper())
            return key1;
        else
        {
            RegistryKey mReturn = SearchSubKey(key1, KeyName);
            if (mReturn != null)
                return mReturn;
        }
    }
    return null;
}

Call this function as

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Control\Power\PowerSettings\");
RegistryKey SubKey = SearchSubKey(key, "0cc5b647-c1df-4637-891a-dec35c318583");

This function search through all sub keys in main key recursively. If you want to search up to particular level then you have to add that logic in function.

OTHER TIPS

I guess you forgot to mention the Registry key name in a proper fashion. Here is the piece of code which brings out the output :

class Program
{
    static void Main(string[] args)
    {
        RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion");
        foreach (var v in key.GetSubKeyNames())
        {
            RegistryKey key1 = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\" + v);
            foreach ( var v1 in key1.GetSubKeyNames())
            {
                if (v1 == "{00EC8ABC-3C5A-40F8-A8CB-E7DCD5ABFA05}")
                Console.WriteLine(key1);
            }
        }
    }
}

Please let me know if this works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top