简体   繁体   English

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

[英]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. 我在Google上搜索了很多,但找不到任何更改子项的方法。

Is Microsoft supporting this feature? Microsoft是否支持此功能?

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. 您正在寻找RegistryKey.CreateSubKey方法。

Example (from http://msdn.microsoft.com/en-us/library/ad51f2dx.aspx ): 示例(来自http://msdn.microsoft.com/zh-cn/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. Regedit可以通过创建一个新的键并将每个子键和值分别复制到新的键来重命名键。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM