Edit Registry Keys

∥☆過路亽.° 提交于 2019-12-11 02:38:11

问题


i need to edit those registry keys

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SafeBoot\Minimal
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SafeBoot\Network

to

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SafeBoot\Minimal-
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SafeBoot\Network-

I tried with

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\SafeBoot\Minimal", true);

but then there is not rename,copy method.


回答1:


From Copy and Rename Registry Keys project:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

// RenameRegistryKey © Copyright 2006 Active Computing
namespace RenameRegistryKey
{
    public class RegistryUtilities
    {
        /// <summary>
        /// Renames a subkey of the passed in registry key since 
        /// the Framework totally forgot to include such a handy feature.
        /// </summary>
        /// <param name="regKey">The RegistryKey that contains the subkey 
        /// you want to rename (must be writeable)</param>
        /// <param name="subKeyName">The name of the subkey that you want to rename
        /// </param>
        /// <param name="newSubKeyName">The new name of the RegistryKey</param>
        /// <returns>True if succeeds</returns>
        public bool RenameSubKey(RegistryKey parentKey, 
            string subKeyName, string newSubKeyName)
        {
            CopyKey(parentKey, subKeyName, newSubKeyName);
            parentKey.DeleteSubKeyTree(subKeyName);
            return true;
        }

        /// <summary>
        /// Copy a registry key.  The parentKey must be writeable.
        /// </summary>
        /// <param name="parentKey"></param>
        /// <param name="keyNameToCopy"></param>
        /// <param name="newKeyName"></param>
        /// <returns></returns>
        public bool CopyKey(RegistryKey parentKey, 
            string keyNameToCopy, string newKeyName)
        {
            //Create new key
            RegistryKey destinationKey = parentKey.CreateSubKey(newKeyName);

            //Open the sourceKey we are copying from
            RegistryKey sourceKey = parentKey.OpenSubKey(keyNameToCopy);

            RecurseCopyKey(sourceKey, destinationKey);

            return true;
        }

        private void RecurseCopyKey(RegistryKey sourceKey, RegistryKey destinationKey)
        {
            //copy all the values
            foreach (string valueName in sourceKey.GetValueNames())
            {        
                object objValue = sourceKey.GetValue(valueName);
                RegistryValueKind valKind = sourceKey.GetValueKind(valueName);
                destinationKey.SetValue(valueName, objValue, valKind);
            }

            //For Each subKey 
            //Create a new subKey in destinationKey 
            //Call myself 
            foreach (string sourceSubKeyName in sourceKey.GetSubKeyNames())
            {
                RegistryKey sourceSubKey = sourceKey.OpenSubKey(sourceSubKeyName);
                RegistryKey destSubKey = destinationKey.CreateSubKey(sourceSubKeyName);
                RecurseCopyKey(sourceSubKey, destSubKey);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/14170886/edit-registry-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!