Search code examples
c#registryregistrykey

How to edit registry value when I dont know full name of a field?


here

On the screenshot is what I am dealing with basically I know don't know the second part of the name before my game launches because its generated randomly based on monitor ID or something.

  1. I would like to check if the key exist by partial name
  2. Get full name if its found
  3. Set my value for it

Currently I use this and its simply not possible to do it this way I think.

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\AAA\MyApp\test\", false);

if (key != null)
{
    return (string)key.GetValue("Screenmanager Resolution ....");
}
else
{
    return null;
}

I would then like to set the value myself like

key.SetValue("Screenmanager Resolution Width_h182942802", 1024, RegistryValueKind.DWord);

Solution

  • You can get the value names with GetValueNames() and then see if the "Screen Resolution *" value exists:

    string[] valueNames = key.GetValueNames();
    string valueName = valueNames.FirstOrDefault(s => s.StartsWith("Screenmanager Resolution Width_"));
    if(valueName != null)
    {
        return key.GetValue(valueName);
    }
    

    or without LINQ:

    foreach(string valueName in key.GetValueNames())
    {
        if(valueName.StartsWith("Screenmanager Resolution Width_"))
            return key.GetValue(valueName);
    }
    return null;