Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Registry Key exists?

Status
Not open for further replies.

croydon

Programmer
Apr 30, 2002
253
EU
Is there a way to determine whether a specific registry key exists?

I want to check whether the key exists, and if not create it.

Thanks.
 
I am already using Microsoft.Win32.RegistryKey to read the registry values.

You can obviously create and delete the key here but there seems no way to check whether it already exists.
 
You can either create and object and test it for "Is Nothing" or more common in your catch handling grab the ArgumentException.

Code:
Dim key As Object = Registry.LocalMachine.OpenSubKey("RegistryRightsExample", True)
If key Is Nothing Then
   MsgBox("does not exist")
End If

or
Code:
        Try
            Registry.LocalMachine.DeleteSubKey("RegistryRightsExample")
            
        Catch ex As ArgumentException
            MsgBox("does not exist")
        Catch ex As Exception
            MsgBox("Unable to delete the example key: {0}")
            Return
        End Try

Note:TipGiver was on the ball. I knew nothing of this until reading briefly up on it @
Future reference try that first :)

General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top