The process of renaming a registry key is actually a recursive copy of all
the values and sub keys and then a delete of the original key. So when you
call RenameSubKey it actually calls CopyKey.
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Win32
Namespace RenameRegisTryKey
Public Class RegisTryUtilities
Public Function RenameSubKey(ByVal parentKey As RegistryKey, ByVal
subKeyName As String, ByVal NewSubKeyName As String) As Boolean
CopyKey(parentKey, subKeyName, NewSubKeyName)
parentKey.DeleteSubKeyTree(subKeyName)
Return True
End Function
Public Function CopyKey(ByVal parentKey As RegistryKey, ByVal
keyNameToCopy As String, ByVal NewKeyName As String) As Boolean
'Create new key
Dim destinationKey As RegistryKey =
parentKey.CreateSubKey(NewKeyName)
'Open the sourceKey we are copying from
Dim sourceKey As RegistryKey =
parentKey.OpenSubKey(keyNameToCopy)
RecurseCopyKey(sourceKey, destinationKey)
Return True
End Function
Private Sub RecurseCopyKey(ByVal sourceKey As RegistryKey, ByVal
destinationKey As RegistryKey)
'copy all the values
Dim valueName As String
For Each valueName In sourceKey.GetValueNames()
Dim objValue As Object = sourceKey.GetValue(valueName)
Dim valKind As RegistryValueKind =
sourceKey.GetValueKind(valueName)
destinationKey.SetValue(valueName, objValue, valKind)
Next
'For Each subKey
'Create a new subKey in destinationKey
'Call myself
Dim sourceSubKeyName As String
For Each sourceSubKeyName In sourceKey.GetSubKeyNames()
Dim sourceSubKey As RegistryKey =
sourceKey.OpenSubKey(sourceSubKeyName)
Dim destSubKey As RegistryKey =
destinationKey.CreateSubKey(sourceSubKeyName)
RecurseCopyKey(sourceSubKey, destSubKey)
Next
End Sub
End Class
End Namespace