public static bool RenameValueSafe(this RegistryKey key, string oldName, string newName) { try { key.CopyValue(oldName, newName); key.DeleteValue(oldName); return(true); } catch { key.DeleteValueSafe(newName); return(false); } }
/// <summary> /// Attempts to rename a registry value to the key provided using the specified old /// name and new name. /// </summary> /// <param name="key">The key of which the registry value is to be renamed from.</param> /// <param name="oldName">The old name of the registry value.</param> /// <param name="newName">The new name of the registry value.</param> /// <returns>Returns boolean value if the action succeded or failed; Returns /// </returns> public static bool RenameValueSafe(this RegistryKey key, string oldName, string newName) { try { //Copy from old to new key.CopyValue(oldName, newName); //Despose of the old value key.DeleteValue(oldName); return(true); } catch { //Try to despose of the newKey (The rename failed) key.DeleteValueSafe(newName); return(false); } }
/// <summary> /// Renames the specified value /// </summary> /// <param name="registryKey">The parent registry key</param> /// <param name="valueName">The name of the value to rename</param> /// <param name="newValueName">The new name of the value</param> public static void MoveValue(this RegistryKey registryKey, string valueName, string newValueName) { if (String.IsNullOrEmpty(valueName)) { throw new ArgumentException(); } if (String.IsNullOrEmpty(newValueName)) { throw new ArgumentException(); } // Copy the value registryKey.CopyValue(valueName, newValueName); // Delete the old value registryKey.DeleteValue(valueName); }