[2005] registry

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
how can i scan the registry recursively, getting all subkeys + values, then adding them to a list? (listbox, listview or just a textbox)
 
Take a look at the attached project, it display a common technique for presenting large tree structures of data to user, such as the Windows registry hives with all keys. Only what the user need to see is processed at any time. That means initially only the first two levels of treenodes/keys are added (base keys and first childs). When user clicks to expand one branch the childs are already there, but now more information is needed, user need to know if any of the childs have childs of their own so further navigation can be done - the child nodes of each child of this branch is therefore processed for each expansion (done in AfterExpand event for better reponse). This processing is only done once, processed nodes are marked and next time same node is expanded nothing needs to be done. The result of this processing is that user don't have to wait a minute or more for the whole registry to be examined for large amounts of data the user never was interested in, and UI is immediately ready. Same goes for the NameValue pairs (the ListView), here in this example NameValues are retrieved only when a node is selected.
 

Attachments

  • vbnet20-RegistryWork.zip
    16.7 KB · Views: 32
thanks john. what i was trying to do is read the registry, and store into a list. then read it again after i'd added an email account to outlook express, to see which keys/values had changed.
so ultimately what i was trying to do is write an application that adds an email account to outlook express. any ideas about that?
 
The concept of solving that is quite easy. Output one text line for each NameValue (including it's path) in registry to make a snapshot. Then you compare each text line in new file with all lines in the old, removing those that haven't changed.

Here is an example of recursive method to traverse the registry, first call you provide empty path (or hive name) and one of the base keys.
VB.NET:
Sub addKeys(ByVal w As IO.StreamWriter, ByVal path As String, ByVal regkey As RegistryKey)
    Try
        Dim format As String = "{0}|||{1}|||{2}|||{3}"
        For Each name As String In regkey.GetValueNames
            w.WriteLine(String.Format(format, path, name, regkey.GetValueKind(name).ToString, ConvertValueToSimpleString(regkey.GetValue(name))))
        Next
        For Each key As String In regkey.GetSubKeyNames
            addKeys(w, path & "\" & key, regkey.OpenSubKey(key))
        Next
        regkey.Close()
    Catch ex As Exception
    End Try
End Sub
'ConvertValueToSimpleString' is pseudo code, see the first example how you can work with different value types to create a simple string value.

The basic comparison implementation is to use StreamReader to read all lines of the newer snapshot into a List(Of String) for example named 'newlines', then read all lines from older snapshot with a new StreamReader and do this operation:
VB.NET:
newlines.Remove(reader.ReadLine)
When this is finished the newlines list contains only lines that are new or changed compared to the older snapshot.

Mind you, the amount of registry data is massive, perhaps you should create one file for each hive.
 
For faster processing it should be sufficient to scan the hives CurrentUser and LocalMachine. Much registry information is mirrored, for example the large ClassesRoot is a special "merge view" of info from parts of the two mentioned hives. More info about this in Windows registry information for advanced users and About the Registry.
 
Back
Top