使用C#修改注册表中的子键

时间:2011-11-04 09:28:53

标签: c# registry

我需要更改注册表中的子项。

我搜索了很多内容,但没有找到任何方法来更改子键。

Microsoft是否支持此功能?

我可以使用此

更改键/值对,但不能更改子键
Registry.SetValue(keypath, subkey, Guid.NewGuid().ToString("B"));

4 个答案:

答案 0 :(得分:1)

您想要在子项上更改什么?如果是名称,则为只读且无法更改。 Regedit可以通过创建新密钥并将每个子密钥和值单独复制到新密钥来重命名密钥。

答案 1 :(得分:0)

您正在寻找RegistryKey.CreateSubKey方法。

示例(来自http://msdn.microsoft.com/en-us/library/ad51f2dx.aspx):

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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);
    }
}
相关问题