How to: Read a Value from a Registry Key in Visual Basic

The GetValue method of the My.Computer.Registry object can be used to read values in the Windows registry. If the key, in this case "Software\MyApp", does not exist, an exception is thrown. If the ValueName, in this case "Name", does not exist, Nothing is returned.

To read a value from a registry key

  • Use the GetValue method, specifying the path and name) to read a value from registry key. The following example reads the value Name from HKEY_CURRENT_USER\Software\MyApp and displays it in a message box.

    Dim readValue As String
    readValue = My.Computer.Registry.GetValue _
    ("HKEY_CURRENT_USER\Software\MyApp", "Name", Nothing)
    MsgBox("The value is " & readValue)
    

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Operating System > Registry. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Robust Programming

The registry holds top-level, or root, keys that are used to store data. For instance, the HKEY_LOCAL_MACHINE root key is used for storing machine-level settings used by all users, while HKEY_CURRENT_USER is used for storing data specific to an individual user.

The following conditions may cause an exception:

Security

To run this process, your assembly requires a privilege level granted by the RegistryPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. Similarly, the user must have the correct ACLs for creating or writing to settings. For example, a local application that has the code access security permission might not have operating system permission. For more information, see Code Access Security Basics.

See Also

Tasks

Troubleshooting: Manipulating the Registry

Concepts

Common Registry Tasks

Reference

My.Computer.Registry Object

RegistryHive

Other Resources

Walkthrough: Creating a Registry Key and Changing Its Values