![]() |
|||||||
| 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 |
|||||||






Wolf Tracks A Q&A Forum |