Export (0) Print
Expand All
Code: Finding the Names of Resources in an Assembly (Visual Basic)
Code: Listing the Colors in the Color Structure (Visual Basic)
Code: Retrieving an Image that Is an Embedded Resource (Visual Basic)
Code: Running a Program (Visual Basic)
Code: Running a Program Associated with a File Type (Visual Basic)
Code: Sending an E-Mail Message (Visual Basic)
Expand Minimize

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:
© 2014 Microsoft