Question

Using VS2013 and VB.NET (Framework 4.5), and no matter what I do, I can't detect if a key value exists. I'm trying to detect if a value exists, if it does exist update with a value from a combobox or add it if it doesn't exit.

Key is HKEY_USERS\S-1-5-18\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device.

The code I've been tinkering with is below. Any thoughts?

Dim l_subKeyPath As String = "HKEY_USERS\S-1-5-18\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
Dim l_regKey = My.Computer.Registry.CurrentUser.OpenSubKey(l_subKeyPath)

If l_regKey Is Nothing Then
   ' if nothing then key doesn't exists
  Else
   ' key exists
End If

The code always returns 'Is Nothing' even when the key exists.

Was it helpful?

Solution

Try to do this.

Dim l_subKeyPath As String = "Software\Microsoft\Windows NT\CurrentVersion\Windows"
Dim l_regKey = My.Computer.Registry.CurrentUser.OpenSubKey(l_subKeyPath)

If l_regKey Is Not Nothing Then
   Dim deviceValue as String =   l_regKey.GetValue("Device")
  // 
Else
End If

Your code has problem as you have provided complete path of registry key with its value. Also your code contain HKEY_USers\S-1-5-18 which is not required if you use Registry.CurrentUser

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top