Rename registry Key [folder/ branch]

  • Thread starter Thread starter hadarmorchi
  • Start date Start date
H

hadarmorchi

hello fellows,
does anyone have a code that do the following :
Rename registry Key [folder/ branch].

Please, if anyone have a vb.net 2003 code sample

10x in advance
Hadar
 
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
 
Okay I see you need it 1.1. Generics wont work nor will value valuekind. So
here is my best guess. Be careful with this please as I can't really test it
here.

Imports System

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 Object = sourceKey.GetValue(valueName)

destinationKey.SetValue(valueName, objValue)

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
 
Back
Top