How to: Determine if a Value Exists in a Registry Key in Visual Basic

The GetValue method of the My.Computer.Registry object can be used to determine whether a given value exists in a specific registry key.

When reading the registry from a Web application, the current user depends on the authentication and impersonation implemented in the Web application.

To determine whether a value exists in a registry key

  • Use the GetValue method to retrieve the value. The following code checks the value and returns a message if it does not exist.

    If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\TestApp",
    "TestValue", Nothing) Is Nothing Then
      MsgBox("Value does not exist.")
    End If

The following conditions may cause an exception:

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 access control lists 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.

Community Additions