简体   繁体   中英

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"));

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");

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.

如果要更改“默认值”,请在设置时使用空白字符串作为值名称。

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);
    }
}

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