Microsoft & .NET Visual Basic Using the Registry to Persist Data

Using the Registry to Persist Data

The Registry is a great place to save your application’s options, and user
settings. And the great thing is that VB provides 4 easy functions to access it.  
This is the first in two article explaining how to use the Windows Registry.

Using SaveSetting and GetSetting

This function saves some data into the Registry database.

SaveSetting appname, section, key, setting

appname
Before you start saving data into the Registry, you must decide on an appname which will
be used throughout the application. The registry can be thought of as a computer. The
appname is the equivalent of a drive, for the sake of these functions, although we will
see later that this is not entirely the case. This must be a string, although it can be a
number converted by the CStr function.  Generally, the appname should remain constant
throughout your program.

section
The section is the ‘directory’ of this model. Just like directories on a computer, it can
contain a "" to signify subdirectories. To save the key in the ‘root’ section,
pass vbNullChar for this parameter.

key
The key is the ‘file’ of this model

setting
This is the information in the file. It can be a number or a string.

GetSetting

This function retrieves data from the registry database.

GetSetting(appname, section, key[, default])

appname, section and key are all the same as above

default
If Visual Basic cannot find the specified key, it will return the contents of default
instead.  This is optional, but useful for error checking in case the settings have
not been saved before.  If this is not included, and there is no data saved, the
function will return a zero length string.

For example:

SaveSetting "Logon Master", "UsersUser0", _
"Name", "John"
' This has saved "Logon MasterUsersUser0Name" 
' with the data "John"

Debug.Print GetSetting("Logon Master", _
"UsersUser0", "Name", "Anonymous"
' This prints "John"

Debug.Print GetSetting("Logon Master", _
"UsersUser1", "Name", "Anonymous"
' However, this would print "Anonymous" 
' because there is no data saved in 
' "Logon MasterUsersUser1Name"

DeleteSetting

We must have a function to remove entries in the registry. It is the
DeleteSetting function:

DeleteSetting appname[, section[, key]]

This function is used to remove keys and sections. If you want to remove a
key, you must specify the key and section parameters. To remove a whole section, you must
specify the section parameter. The sub sections and sub keys are automatically deleted as
well – you do not need to delete each individual key and section. To delete the entire
application’s data, just specify the appname.

For example:

DeleteSetting "Logon Master", "UsersUser0", "Name"
' This will delete the Name key in 
' "Logon MasterUsersUser0"
DeleteSetting "Logon Master", "Users"
' This will delete all sections in 
' "Logon MasterUsers"
DeleteSetting "Logon Master"
' This will delete everything under "Logon Master"

GetAllSettings

This function will, as its name suggests, retrieve all keys in a certain
section.

GetAllSettings(appname, section)

The parameters are as they are for all the functions. However, what this
function does is more complicated. This function returns an array of variants containing
the names and values of all the keys. The best way to explain this is by example:

' Variant to hold 2-dimensional 
' array returned by GetAllSettings
' Integer to hold counter.
Dim MySettings As Variant, intSettings As Integer

' Place some settings in the registry.
SaveSetting "Logon Master", "UsersUser0", _
"Name", "John"
SaveSetting "Logon Master", "UsersUser0", _
"Priviledges", "Supervisor"

' Retrieve the settings.
MySettings = GetAllSettings("Logon Master", _
"UsersUser0")

' Display the settings
For intSettings = LBound(MySettings, 1) _
To UBound(MySettings, 1)

' MySettings(x,0) contains the name of the key, 
' and MySettings(x,1) contains the value
Debug.Print MySettings(intSettings, 0), _
MySettings(intSettings, 1)
Next intSettings

DeleteSetting "Logon Master", vbNullChar

Note: GetAllSettings does not return sub-sections, only keys.

Well that just about sums up the intrinsic VB registry functions. In the
net article, we will look deeper at the registry, using API functions, and how it actually
works, rather than from just VB’s view point. We will see that VB is actually saving the
data in "HKEY_CURRENT_USERSoftwareVB and VBA Program Settings…". We will
see how to access the rest of the registry.

As ever, be careful before poking round the registry with regedit.exe. It is
easy to make mistakes and inadvertantly wipe most of the data, and all of the
functionality of your machine. Be careful!

Latest Posts

Related Stories