public static bool RenameSubKeySafe(this RegistryKey key, string oldName, string newName) { try { key.CopyKey(oldName, newName); key.DeleteSubKeyTree(oldName); return(true); } catch { key.DeleteSubKeyTreeSafe(newName); return(false); } }
/* * Derived and Adapted from drdandle's article, * Copy and Rename Registry Keys at Code project. * Copy and Rename Registry Keys (Post Date: November 11, 2006) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * This is a work that is not of the original. It * has been modified to suit the needs of another * application. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * First Modified by StingRaptor on January 21, 2016 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Original Source: * http://www.codeproject.com/Articles/16343/Copy-and-Rename-Registry-Keys */ /// <summary> /// Attempts to rename a sub-key to the key provided using the specified old /// name and new name. /// </summary> /// <param name="key">The key of which the subkey is to be renamed from.</param> /// <param name="oldName">The old name of the sub-key.</param> /// <param name="newName">The new name of the sub-key.</param> /// <returns>Returns boolean value if the action succeded or failed; Returns /// </returns> public static bool RenameSubKeySafe(this RegistryKey key, string oldName, string newName) { try { //Copy from old to new key.CopyKey(oldName, newName); //Despose of the old key key.DeleteSubKeyTree(oldName); return(true); } catch { //Try to despose of the newKey (The rename failed) key.DeleteSubKeyTreeSafe(newName); return(false); } }
protected void ParseAppVBinaryRegValues(string fileName) { if (string.IsNullOrWhiteSpace(fileName) || (!File.Exists(fileName))) { return; } string tempDestRegBinFile = string.Format("{0}\\DestRoot.dat", Path.GetTempPath()); File.Copy(PathManager.Init.GetResourcePath(PathManager.REGHIVE_FILE), tempDestRegBinFile, true); using (RegistryKey srcRootKey = RegistryNativeMethods.RegLoadAppKey(fileName)) using (RegistryKey destRootKey = RegistryNativeMethods.RegLoadAppKey(tempDestRegBinFile, RegistryNativeMethods.RegSAM.KEY_ALL_ACCESS)) { if (srcRootKey != null && destRootKey != null) { using (RegistryKey sourceKey = srcRootKey.OpenSubKey("REGISTRY\\USER\\[{AppVCurrentUserSID}]", false)) using (RegistryKey destKey = destRootKey.CreateSubKey("HKCU")) { sourceKey.CopyKey(destKey); } using (RegistryKey sourceKey = srcRootKey.OpenSubKey("REGISTRY\\USER\\[{AppVCurrentUserSID}]_Classes", false)) using (RegistryKey destKey = destRootKey.CreateSubKey(@"HKCU\Software\Classes")) { sourceKey.CopyKey(destKey); } using (RegistryKey sourceKey = srcRootKey.OpenSubKey("REGISTRY\\MACHINE", false)) using (RegistryKey destKey = destRootKey.CreateSubKey("HKLM")) { sourceKey.CopyKey(destKey); } string tempKey = ""; AddRecurseRegValue(destRootKey, tempKey); } } }
/// <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 static bool RenameSubKey(this RegistryKey parentKey, string subKeyName, string newSubKeyName) { parentKey.CopyKey(subKeyName, newSubKeyName); parentKey.DeleteSubKeyTree(subKeyName); return(true); }