Microsoft KB Archive/316151

From BetaArchive Wiki

Article ID: 316151

Article Last Modified on 12/6/2006



APPLIES TO

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition



This article was previously published under Q316151

For a Microsoft Visual Basic 6.0 version of this article, see 145679.

IN THIS TASK

SUMMARY

REFERENCES

SUMMARY

This article demonstrates how to use Visual Basic. NET or Visual Basic 2005 to access the registry. To do this, you write your own registry accessing functions by using the Base Class Libraries.

Limitations of the GetSetting and SaveSetting Functions

Visual Basic .NET provides the GetSetting and SaveSetting functions to access the registry. These functions have the following limitations:

  • You can access only the registry keys under HKEY_CURRENT_USER\Software\VB and VBA Program Settings.
  • You must be logged on to the system, because you perform the modification under the HKEY_CURRENT_USER registry key. This key is not active until the user is logged on to the system.

NOTE: If the key setting cannot be saved for any reason, an error message appears.

back to the top

Using the Registry and RegistryKey Classes

To access the registry without the constraints of the GetSetting and SaveSetting functions, use the Registry and RegistryKey classes in the Microsoft.Win32 namespace. The Registry class supplies the base registry keys to access values and subkeys in the registry. It has static (shared) fields, such as CurrentUser, to represent the root registry key.

The RegistryKey class represents a key level node in the Windows registry. This class is a registry encapsulation that enables read and write permissions to the registry through the GetValue and SetValue methods of the RegistryKey class.

To create a console project to read and write to the registry by using the Registry and RegistryKey classes, follow these steps:

  1. Open Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New Project.
  3. Select Visual Basic Projects in the TreeView on the left, and then select Console Application in the ListView on the right.

    Note In Visual Studio 2005, select Visual Basic in the Project types on the left, and then select Console Application in the Templates on the right.
  4. Import the following namespaces to the project:

    Imports System.Diagnostics
    Imports Microsoft.Win32
    
                            
  5. Add the following code to the Module1:

    Sub WriteRegistry(ByVal ParentKey As RegistryKey, ByVal SubKey As String, _
        ByVal ValueName As String, ByVal Value As Object)
    
        Dim Key As RegistryKey
    
        Try
            'Open the registry key.
            Key = ParentKey.OpenSubKey(SubKey, True)
            If Key Is Nothing Then 'if the key doesn't exist.
                Key = ParentKey.CreateSubKey(SubKey)
            End If
    
            'Set the value.
            Key.SetValue(ValueName, Value)
    
            Console.WriteLine("Value:{0} for {1} is successfully written.", Value, ValueName)
        Catch e As Exception
            Console.WriteLine("Error occurs in WriteRegistry" & e.Message)
        End Try
    End Sub
    
    Sub ReadRegistry(ByVal ParentKey As RegistryKey, ByVal SubKey As String, _
        ByVal ValueName As String, ByRef Value As Object)
    
        Dim Key As RegistryKey
    
        Try
            'Open the registry key.
            Key = ParentKey.OpenSubKey(SubKey, True)
            If Key Is Nothing Then 'if the key doesn't exist
                Throw New Exception("The registry key doesn't exist")
            End If
    
            'Get the value.
            Value = Key.GetValue(ValueName)
    
            Console.WriteLine("Value:{0} for {1} is successfully retrieved.", Value, ValueName)
        Catch e As Exception
            Console.WriteLine("Error occurs in ReadRegistry" & e.Message)
        End Try
    End Sub
    
                            
  6. Copy and paste the following code to replace the Sub Main that is generated by Visual Basic .NET or Visual Basic 2005:

    Sub Main()
        WriteRegistry(Registry.CurrentUser, "Software\MySoftware", "Count", 123)
    
        Dim Value As Object
        ReadRegistry(Registry.CurrentUser, "Software\MySoftware", "Count", Value)
    
        Console.ReadLine()
    End Sub
                            
  7. Press F5 to run the application.

NOTE: This procedure creates a registry key named MySoftware under the subkey HKEY_CURRENT_USER\Software. And it creates a DWORD value named Count with the value 123 under the MySoftware key.

back to the top

REFERENCES

For additional information about the classes and namespaces used in this article, visit the following Microsoft Web sites:

back to the top

Keywords: kbvs2005swept kbvs2005applies kbhowtomaster KB316151