How to: Delete a Registry Key in Visual Basic
The DeleteSubKey(String) and DeleteSubKey(String, Boolean) 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( "Software\TestApp")
Robust Programming
The DeleteSubKey
method returns an empty string if the key/value pair does not exist.
The following conditions may cause an exception:
The name of the key is
Nothing
(ArgumentNullException).The user does not have permissions to delete registry keys (SecurityException).
The key name exceeds the 255-character limit (ArgumentException).
The registry key is read-only (UnauthorizedAccessException).
.NET Framework 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.