/// <summary> /// Renames the specified sub key /// </summary> /// <param name="registryKey">The parent registry key</param> /// <param name="subKeyName">The name of the sub key to rename</param> /// <param name="newSubKeyName">The new name of the key</param> public static void MoveSubKey(this RegistryKey registryKey, string subKeyName, string newSubKeyName) { // Make sure the new name doesn't exist if (RegistryHelpers.KeyExists(RegistryHelpers.CombinePaths(registryKey.Name, newSubKeyName), registryKey.View)) { throw new Exception($"The key {newSubKeyName} already exists"); } // Make sure the original key exists if (!RegistryHelpers.KeyExists(RegistryHelpers.CombinePaths(registryKey.Name, subKeyName), registryKey.View)) { throw new Exception($"The key {subKeyName} does not exist"); } try { // Copy the key registryKey.CopySubKey(subKeyName, newSubKeyName); } catch (Exception ex) { // Delete copied key registryKey.DeleteSubKeyTree(newSubKeyName, false); throw new Exception("Failed to copy sub key tree. Operation failed.", ex); } try { // Delete the old key registryKey.DeleteSubKeyTree(subKeyName); } catch (Exception ex) { throw new Exception("Failed to fully delete old key", ex); } }