2013-01-05 123 views
2

我需要修改这些注册表项编辑注册表项

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

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

我试着用

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

但当时没有重命名,复制方法。

回答

6

Copy and Rename Registry Keys项目:

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

都不错,但我不能写,我得到一个异常 –

+0

你确定这个异常不被许可的问题,原因在哪里?此代码适用于我的机器。 –

+0

是的这是一个权限问题,但我试图通过以管理员身份运行它,仍然是相同的 –