James Stafford
James Stafford

Reputation: 1054

vb.net check if registry subkey value exists

I have an winform which saves registration data to the registry under "Ddoe" - however I need to check if the value has previously been set (created) in doing so I attempted to do:

If (Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HKEY_CURRENT_USER\Software\FCRA\Assignment").GetValue("Ddoe") Is Nothing Then
'doesnt exist
else
'exists
end if

however this is not functioning properly

I am receiving an error

An unhandled exception of type 'System.NullReferenceException' occurred in #appname

it is showing this error at the if statement line

is there a better way to check for the existence of this registry entry or am I going about this all wrong

Upvotes: 3

Views: 5529

Answers (1)

Victor Zakharov
Victor Zakharov

Reputation: 26454

I think the problem is that "HKEY_CURRENT_USER\Software\FCRA\Assignment" does not exist, so OpenSubKey returns Nothing. You cannot use any methods on a Nothing value. Try this one:

If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\FCRA\Assignment",
                                 "Ddoe", Nothing) Is Nothing Then
  'value does not exist
End If

Taken from here:

Upvotes: 6

Related Questions