Recursive Registry Loop

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

I simply want to loop through a registry key, and have all subkeys and
values outputed to the console window. It needs to be recursive, so it keeps
going into each key one at a time, until it finishes. My code is not close.
Anybody have code that has done this?

Dim GroupID As Long = 39

'Dim RegKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\LanmanS­erver\Shares")

Dim RegKey As RegistryKey =
Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("VB and VBA Program
Settings").OpenSubKey("iGen").OpenSubKey("Group").OpenSubKey(GroupID).OpenSubKey("Printer
Settings")

Dim tempKey As RegistryKey

Do Until RegKey.SubKeyCount = 0

Console.WriteLine("There are {0} subkeys under " & RegKey.Name & ".",
RegKey.SubKeyCount.ToString())

Console.WriteLine(vbCrLf & "There are {0} values for " & "{1}.",
RegKey.ValueCount.ToString(), RegKey.Name)

For Each valueName As String In RegKey.GetValueNames()

Console.WriteLine("{0,-8}: {1}", valueName,
RegKey.GetValue(valueName.ToString))

Next



For Each subKeyName As String In RegKey.GetSubKeyNames()

tempKey = RegKey.OpenSubKey(subKeyName)

Console.WriteLine(vbCrLf & "There are {0} values for " & "{1}.",
tempKey.ValueCount.ToString(), tempKey.Name)

For Each valueName As String In tempKey.GetValueNames()

Console.WriteLine("{0,-8}: {1}", valueName,
tempKey.GetValue(valueName.ToString))

Next

Next

If Not tempKey Is Nothing Then RegKey = tempKey

Loop

Console.ReadLine()

End Sub
 
Derek Hart said:
I simply want to loop through a registry key, and have all subkeys
and values outputed to the console window. It needs to be recursive,
so it keeps going into each key one at a time, until it finishes. My
code is not close. Anybody have code that has done this?

Write a Sub that expects a RegKey object, and
1. outputs all values
2. retrieves the sub key names
3. loops over the sub key names and opens each one, and calls the same
Sub again passing the new RegKey just created.

Just translate to VB. That's all.

Isn't your question the answer? :-)


Armin
 
Derek Hart said:
Been going in circles. If anybody has a code example, much
appreciated!

Untested:

Private Shared Sub OutputRegKey( _
ByVal RegKey As Microsoft.Win32.RegistryKey)

For Each ValueName In RegKey.GetValueNames
Debug.Print(RegKey.GetValue(ValueName).ToString)
Next

For Each KeyName In RegKey.GetSubKeyNames
Using Key = RegKey.OpenSubKey(KeyName, False)
OutputRegKey(Key)
End Using
Next

End Sub


Call:

OutputRegKey( _
Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
"SYSTEM\CurrentControlSet\Services\LanmanS­erver\Shares" _
) _
)



AZ
 
Back
Top