How to: Delete a Registry Key in Visual Basic

The DeleteSubKey and DeleteSubKey methods can be used to delete registry keys.

Procedure

To delete a registry key

  • Use the DeleteSubKey method to delete a registry key. This example deletes the key Software/TestApp in the CurrentUser hive. You can change this in the code to the appropriate string, or have it rely on user-supplied information.

    My.Computer.Registry.CurrentUser.DeleteSubKey _
        ("HKEY_CURRENT_USER\Software\TestApp")
    

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 DeleteSubKey method returns an empty string if the key/value pair does not exist.

The following conditions may cause an exception:

Security

Registry calls fail if either sufficient run-time permissions are not granted (RegistryPermission) or if the user does not have the correct access (as determined by the 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

Tasks

Troubleshooting: Manipulating the Registry

Concepts

Security and the Registry

Common Registry Tasks

Reference

DeleteSubKey

DeleteSubKey

RegistryKey

Other Resources

Walkthrough: Creating a Registry Key and Changing Its Values