Accessing the Registry with VB6 and VB.NET

WEBINAR:On-Demand

Storing and retrieving information from the Windows Registry will play a major part in any programmer's life. You can store small bits of information, such as recently used files, form sizes and location, and user preferences—to name just a few—in the Windows Registry. My aim with this article is to explain the major differences between the way Visual Basic 6 and Visual Basic .NET utilise the Windows Registry.

Visual Basic 6 and the Windows Registry

Visual Basic 6 comes with four procedures for accessing the Windows Registry:

GetSetting

You use the GetSetting() function to retrieve a value from a particular section in the Visual Basic key of the Windows Registry.

Syntax

Term

Meaning

StringVariable

String value returned by the GetSetting() function

GetSetting

The name of the function

VBKeyName

String value representing the name of the key within the VB/VBA area of the Registry

Section

String value representing the section or subkey for the specific applications setting

Key

String value that represents the name of a specific entry within the section

Default

Optional argument. A String value that represents the value to return if GetSetting() fails or encounters an error. If the function is successful, it returns the string found at the key setting; if it is unsuccessful, it returns the string value assigned to the Default argument

Sample Application

The first sample application (RegistryVB6) accompanying this article shows in detail how the intrinsic Visual Basic 6 Registry functions are used to store and retrieve values in the Registry. The sample application also shows in detail how to use the necessary API functions to store values in the Registry and read from the Registry.

On the next page, I will explain how Visual Basic.NET accesses the Windows Registry.

DeleteSetting

Example

DeleteSetting("Registry Example")

As you probably noticed, there virtually is not much difference in the Visual Basic 6 and Visual Basic .NET functions. The only minor difference was with the Multidimensional array declaration; besides that, everything still is the same.

Visual Basic .NET introduces two new intrinsic Registry functions, namely UserAppDataRegistry and CommonAppDataRegistry.SetValue, that also store and retrieve values in the Registry. Take a closer look at these.

CommonAppDataRegistry

The CommonAppDataRegistry property returns the Registry key that is used to store non-user specific and non-roaming data for the machine; the Registry key takes the form:

UserAppDataRegistry

The UserAppDataRegistry property returns the Registry key that is used to store roaming data (settings that are specific to a user but machine independent) for each user; the Registry key takes the form:

UserAppDataRegistry.SetValue

UserAppDatRegistry.GetValue

Example

'Variable to store value read from Registry
Dim strFormText As String
'Determine whether or not the Form Display Key actually exists
If Not (Application.UserAppDataRegistry.GetValue("Form Display") _
Is Nothing) Then
'If the Form Display Key exists
'Store value read in strFormText
strFormText = Application.UserAppDataRegistry.GetValue _
("Form Display").ToString()
Me.Text = strFormText 'Assign the value to the Form
Else
'The Form Display Key does not exist
Me.Text = "Key Does Not Exist Yet!"
End If

It is always best to determine whether or not the particular Registry setting is present before referencing it. If you were to omit the simple test, the Application will throw an exception.

Thus far, I have covered the Visual Basic .NET intrinsic Registry functions. Now, you may ask, what if you don't want to store your application settings in either of the above-mentioined Registry keys? The answer to your question is the Win32 Namespace included with the .NET Framework; it includes the Registry and RegistryKey classes.

Registry Class

Term

Meaning

ClassesRoot

Defines the types of documents and the properties associated with those types

CurrentConfig

Contains hardware configuration information that is not user-specific

CurrentUser

Contains information about the current user preferences, such as environmental variables

DynData

Contains dynamic Registry data, such as that used by Virtual Device Drivers

LocalMachine

Contains five subkeys (Hardware, SAM, Security, Software, and System) that hold the configuration data for the local machine

PerformanceData

Contains performance information for software components

Users

Contains information about the default user preferences

To make use of the Win32 Registry functions, you need to add the following line on top of your Visual Basic.NET module.

Imports Microsoft.Win32

Once that is done, you can use the Win32 Registry functions as follows:

Sample Application

The second sample application (VBNETRegistry) included with this article demonstrates all the above methods to utilise the Windows Registry productively.

Conclusion

Although the Registry is a big topic to talk about, I hope that this article has been informative about the various options available for utilising the Registry in Visual Basic 6 and Visual Basic .NET productively.

About the Author

Hannes du Preez

Hannes du Preez is a Microsoft MVP for Visual Basic. He is a trainer at a South African-based company. He is the co-founder of hmsmp.co.za, a community for South African developers.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date