Check if a key exists in the Windows Registry with VB.NET
One way is to use the Registry.OpenSubKey
method
If Microsoft.Win32.Registry.LocalMachine.OpenSubKey("TestKey") Is Nothing Then
' Key doesn't exist
Else
' Key existed
End If
However I would advise that you do not take this path. The OpenSubKey
method returning Nothing
means that the key didn't exist at some point in the past. By the time the method returns another operation in another program may have caused the key to be created.
Instead of checking for the key existence and creating it after the fact, I would go straight to CreateSubKey
.