Option Strict On

Imports Microsoft.VisualBasic
Imports Microsoft.Win32
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class UseRegistry

Public WithEvents frm As New Form
Private wsOriginal As FormWindowState

Public Shared Sub Main
 Dim ur As New UseRegistry
 ur.Startup
End Sub

Public Sub Startup

 Dim appKey As RegistryKey

 ' Get WindowState from registry
 appKey = Registry.CurrentUser.CreateSubkey("Software\HowlingWolf\RegistryApp")

 ' Retrieve WindowState
 frm.WindowState = CType(appKey.GetValue("WindowState", _
                         FormWindowState.Normal), FormWindowState)
 wsOriginal = frm.WindowState
 appKey.Close
   
 ' Display form
 frm.ShowDialog()

End Sub

Private Sub frm_Closing(sender As Object, _
                      e As System.ComponentModel.CancelEventArgs) _
          Handles frm.Closing

 Dim obj As Object
 Dim ws As FormWindowState
 Dim res As MsgBoxResult
 Dim valCreate As Boolean

 Dim appKey As RegistryKey = _  
       Registry.CurrentUser.OpenSubkey("Software\HowlingWolf\RegistryApp", _
       True)
   
 obj = appKey.GetValue("WindowState")
 If obj Is Nothing Then
    valCreate = True
 Else
    ws = CType(obj, FormWindowState)
    If frm.WindowState <> ws Then
       res = MsgBox("Save current window state as default? ", vbYesNo, _
                    "Save Window State")
    End if
 End If

 If res = MsgBoxResult.Yes Or valCreate Then
    appKey.SetValue("WindowState", frm.WindowState, RegistryValueKind.DWord)
 End If
 appKey.Close
End Sub

End Class
Note that we use the Registry class to indicate the top-level registry key with which we
wish to work, as well as to obtain a reference to a
RegistryKey object. In this code, the
static
CurrentUser field of the Registry class returns an instance of a
RegistryKey object representing the top-level key, in this case
HKEY_CURRENT_USER. We can then call the CreateSubkey method, which either
creates a new subkey or opens an existing one and returns a
RegistryKey object
representing that key. Next, the
GetValue method is called and its return value assigned to
the form's
WindowState property. If the value does not exist (presumably because the
application is run for the first time), a default value is used isntead.

In the form's
Closing event procedure, we again obtain a reference to our application's
registry key, then attempt to retrieve its
WindowState value by calling the RegistryKey
object's overloaded GetValue method. Since here we want to know whether the value
exists, we don't supply a default value. If the method returns
Nothing, we know we have to
create the named value. Otherwise, we can compare its value to the current value of the form's
WindowState property. If they differ, the application prompts the user to indicate whether
he or she wishes to save the new window state information. If the user indicates yes, or if the
value does not exist, the
WindowState property's value is written to the registry as a
DWORD value.

Hopefully, this succeeds in conveying some sense of how easy it is to work with the .NET
registry classes, as well as its superiority over the intrinsic VB.NET registry access functions.
---------------------------------------------------------------------------------------

View Previous Wolf Tracks Columns

Submit a Question to Wolf Tracks


Registry Access with VB .NET
Q from Isaac H., Peabody, MA:

I've been reading about configuration information that you can extract from the registry, but I
can't figure out how to get at it with the
GetSetting or GetAllSettings function.
What am I missing?
--------------------------------------------------------------------------------
Howling Wolf Consulting Services

Wolf Tracks

A Q&A Forum


View Previous Columns


Submit a Question

The Response:

You're not missing anything, Isaac. You simply can't do it.

With the transition from VB 6.0 to VB .NET, the Visual Basic design team made a mostly
successful effort to modernize and streamline the Visual Basic language. Inexplicably, though,
the VB registry functions survived the cut.  These functions were first introduced in Visual Basic
4.0, which included 16-bit and 32-bit versions. On a 16-bit Windows platform, the functions
worked with initialization files. On a 32-bit Windows platform, the functions accessed the
registry. This accounts for some of awkwardness in these functions' syntax, as well as for the
fact that they treat all registry data as if it were string data.

But more to the point, the functions are intended to access only a portion of the registry. The
idea is that your application will store its own user configuration information in the registry, and
that you will use the registry functions to save, read, and maintain these keys and values. So the
functions work only with subkeys of
HKEY_CURRENT_USER\Software\VB and
VBA Program Settings
. The functions aren't even flexible enough to allow you to
store system settings (which would presumably go in a key named
HKEY_LOCAL_MACHINE\Software\VB and VBA Program Settings).

In a word, the VB registry functions are the next best thing to useless. I would just ignore them.
In VB 6.0 and earlier versions, that necessitated using the registry functions in the Win32 API,
which some developers found painful. But in VB .NET, you can take advantage of the
Registry and RegistryKey classes in the Microsoft.Win32 namespace of the
.NET Framework Class Library.

To see how this works, let's look at a simple application that opens a window. We'll use the
registry to determine whether the window be minized, maximized, or normal. Then, when the
window closes, we'll ask the user whether he or she wants to save the window's current state.
The window's state value will be stored in a value named
WindowState, which is stored in
the
HKEY_CURRENT_USER\Software\HowlingWolf\RegistryApp key.
Here is the code: