Sunil Agarwal
Sunil Agarwal

Reputation: 4277

Modify Subkey in Registry using C#

I need to change the subkey in registry.

I googled a lot and din't found any way to change the subkey.

Is Microsoft supporting this feature?

I am able to change the key/value pair but not subkey using this

Registry.SetValue(keypath, subkey, Guid.NewGuid().ToString("B"));

Upvotes: 2

Views: 2392

Answers (4)

ChrisBD
ChrisBD

Reputation: 9209

static void WriteRegistry(RegistryKey parentKey, String subKey, String valueName, Object value)
{
        RegistryKey key;
    try
    {
        key = parentKey.OpenSubKey(subKey, true);
        if(key == null) //If the key doesn't exist.
                {
           key = parentKey.CreateSubKey(subKey);
                }

        //Set the value.
        key.SetValue(valueName, value);

        Console.WriteLine("Value:{0} for {1} is successfully written.", value, valueName);
    }
    catch(Exception e)
    {
        Console.WriteLine("Error occurs in WriteRegistry" + e.Message);
    }
}

Upvotes: 1

Deanna
Deanna

Reputation: 24283

If you're trying to change the "default value" use a blank string as the value name when setting it.

Upvotes: 2

Deanna
Deanna

Reputation: 24283

What are you trying to change on the subkey? If it's the name, this is read only and can not be changed. Regedit can rename keys by creating a new one and individually copying each sub key and value to the new one.

Upvotes: 1

Peter Olsson
Peter Olsson

Reputation: 1322

You are looking for the RegistryKey.CreateSubKey method.

Example (from http://msdn.microsoft.com/en-us/library/ad51f2dx.aspx):

 RegistryKey test9999 = Registry.CurrentUser.CreateSubKey("Test9999");

Upvotes: 1

Related Questions