LabRat
LabRat

Reputation: 2014

checking for regkey vb.net

I cant get the following code to check if my regestrykey exists can some one help. the idea is to check if it exists if so do nothing if not create it, along with its value...

the full key is HKEY_LOCAL_MACHINE\SOFTWARE\VTS\Advanced Offset 2\Admin\Access

>         If Dir$("HKEY_LOCAL_MACHINE\SOFTWARE\VTS\Advanced Offset 2\Admin", vbDirectory) <> "" Then

        Else
            MsgBox(".NET Framework 2.0")

        End If

UPDATED CODE:

Dim l_subKeyPath As String = "SOFTWARE\VTS\Advanced Offset 2\Admin\Access"
Dim l_regKey As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey(l_subKeyPath, False)
If (l_regKey Is Nothing) Then
    'if nothing then key doesn't exists
    'Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\VTS\Advanced Offset 2\Admin", "ACCESS", "PASSWbORD")
    MsgBox("yess")
Else
    'key exists
    MsgBox("no")
End If

Upvotes: 0

Views: 3823

Answers (2)

Martin
Martin

Reputation: 1448

You say " check if it exists if so do nothing if not create it, along with its value... ". If you reate a key, you'll also need to create a NAME and add a value to the name, you cann't add a value to a key.

Take look at the sample in MSDN OpenSubKey

Upvotes: 0

MichaelS
MichaelS

Reputation: 7103

Try this:

    Dim l_subKeyPath as string = "SOFTWARE\VTS\Advanced Offset 2\Admin\Access"
    Dim l_regKey As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey(l_subKeyPath, False)
     If (l_regKey Is Nothing) Then
        'if nothing then key doesn't exists
     else
        'key exists
     End If

Quoting MSDN:

RegistryKey.OpenSubKey Method

If the requested key does not exist, this method returns Nothing instead of throwing an exception.

Upvotes: 1

Related Questions