This documentation is archived and is not being maintained.

Code: Determining Whether a Registry Key Exists (Visual Basic)

Visual Studio .NET 2003

This example checks to see if the specified key, SOFTWARE\\Microsoft\\TestApp\\1.0, exists and creates it if it does not.

Example

Private Sub DoesKeyExist
    Dim regVersion As Microsoft.Win32.RegistryKey
    regVersion = _
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\TestApp\\1.0", True)
    If regVersion Is Nothing Then
        regVersion = _
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\TestApp\\1.0")
    End If
End Sub

Compiling the Code

This example requires:

  • A reference to the Win32 namespace.

Security

Registry calls fail if either sufficient runtime permissions are not granted (RegistryPermission Class) or if the user does not 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.

See Also

Code: Creating a Key In the Registry (Visual Basic) | Registry Access Changes in Visual Basic .NET | Registry Class | Registry Summary | System Input/Output Example Topics

Show: