|
|
Title | Delete a registry key and its subkeys |
Description | This example shows how to delete a registry key and its subkeys in Visual Basic 6. It uses registry API functions to recursively delete the key's subkeys. It then deletes the key itself. |
Keywords | registry, delete keys, keys |
Categories | Tips and Tricks, Windows |
|
|
Recursively delete any subkeys. Then use the RegDeleteKey API function to delete the key.
WARNING: Messing around with the Registry can be extremely dangerous. If you accidentally delete the wrong part of your registry, you may make your system unbootable.
|
|
' Delete this key.
Private Sub DeleteKey(ByVal section As Long, ByVal key_name _
As String)
Dim pos As Integer
Dim parent_key_name As String
Dim parent_hKey As Long
If Right$(key_name, 1) = "\" Then key_name = _
Left$(key_name, Len(key_name) - 1)
' Delete the key's subkeys.
DeleteSubkeys section, key_name
' Get the parent's name.
pos = InStrRev(key_name, "\")
If pos = 0 Then
' This is a top-level key.
' Delete it from the section.
RegDeleteKey section, key_name
Else
' This is not a top-level key.
' Find the parent key.
parent_key_name = Left$(key_name, pos - 1)
key_name = Mid$(key_name, pos + 1)
' Open the parent key.
If RegOpenKeyEx(section, _
parent_key_name, _
0&, KEY_ALL_ACCESS, parent_hKey) <> _
ERROR_SUCCESS _
Then
MsgBox "Error opening parent key"
Else
' Delete the key from its parent.
RegDeleteKey parent_hKey, key_name
' Close the parent key.
RegCloseKey parent_hKey
End If
End If
End Sub
' Delete all the key's subkeys.
Private Sub DeleteSubkeys(ByVal section As Long, ByVal _
key_name As String)
Dim hKey As Long
Dim subkeys As Collection
Dim subkey_num As Long
Dim length As Long
Dim subkey_name As String
' Open the key.
If RegOpenKeyEx(section, key_name, _
0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS _
Then
MsgBox "Error opening key '" & key_name & "'"
Exit Sub
End If
' Enumerate the subkeys.
Set subkeys = New Collection
subkey_num = 0
Do
' Enumerate subkeys until we get an error.
length = 256
subkey_name = Space$(length)
If RegEnumKey(hKey, subkey_num, _
subkey_name, length) _
<> ERROR_SUCCESS Then Exit Do
subkey_num = subkey_num + 1
subkey_name = Left$(subkey_name, InStr(subkey_name, _
Chr$(0)) - 1)
subkeys.Add subkey_name
Loop
' Recursively delete the subkeys and their subkeys.
For subkey_num = 1 To subkeys.Count
' Delete the subkey's subkeys.
DeleteSubkeys section, key_name & "\" & _
subkeys(subkey_num)
' Delete the subkey.
RegDeleteKey hKey, subkeys(subkey_num)
Next subkey_num
' Close the key.
RegCloseKey hKey
End Sub
|
|
The GetKeyInfo function shown in the following code builds a string describing a key and its subkeys.
|
|
' Get the key information for this key and
' its subkeys.
Private Function GetKeyInfo(ByVal section As Long, ByVal _
key_name As String, ByVal indent As Integer) As String
Dim subkeys As Collection
Dim subkey_values As Collection
Dim subkey_num As Integer
Dim subkey_name As String
Dim subkey_value As String
Dim length As Long
Dim hKey As Long
Dim txt As String
Dim subkey_txt As String
Set subkeys = New Collection
Set subkey_values = New Collection
If Right$(key_name, 1) = "\" Then key_name = _
Left$(key_name, Len(key_name) - 1)
' Open the key.
If RegOpenKeyEx(section, _
key_name, _
0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS _
Then
MsgBox "Error opening key."
Exit Function
End If
' Enumerate the subkeys.
subkey_num = 0
Do
' Enumerate subkeys until we get an error.
length = 256
subkey_name = Space$(length)
If RegEnumKey(hKey, subkey_num, _
subkey_name, length) _
<> ERROR_SUCCESS Then Exit Do
subkey_num = subkey_num + 1
subkey_name = Left$(subkey_name, InStr(subkey_name, _
Chr$(0)) - 1)
subkeys.Add subkey_name
' Get the subkey's value.
length = 256
subkey_value = Space$(length)
If RegQueryValue(hKey, subkey_name, _
subkey_value, length) _
<> ERROR_SUCCESS _
Then
subkey_values.Add "Error"
Else
' Remove the trailing null character.
subkey_value = Left$(subkey_value, length - 1)
subkey_values.Add subkey_value
End If
Loop
' Close the key.
If RegCloseKey(hKey) <> ERROR_SUCCESS Then
MsgBox "Error closing key."
End If
' Recursively get information on the keys.
For subkey_num = 1 To subkeys.Count
subkey_txt = GetKeyInfo(section, key_name & "\" & _
subkeys(subkey_num), indent + 2)
txt = txt & Space(indent) & _
subkeys(subkey_num) & _
": " & subkey_values(subkey_num) & vbCrLf & _
subkey_txt
Next subkey_num
GetKeyInfo = txt
End Function
|
|
|
|
|
|