用VB.NET检测注册表项

时间:2014-04-20 02:30:19

标签: vb.net registry

使用VS2013和VB.NET(Framework 4.5),无论我做什么,我都无法检测是否存在键值。我试图检测一个值是否存在,如果它存在,则使用组合框中的值更新,或者如果它不退出则添加它。

键是HKEY_USERS\S-1-5-18\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device

我一直在修改的代码如下。有什么想法吗?

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

即使密钥存在,代码也总是返回'Is Nothing'。

1 个答案:

答案 0 :(得分:1)

尝试这样做。

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

您的代码存在问题,因为您已提供注册表项的完整路径及其值。 您的代码也包含HKEY_USers \ S-1-5-18,如果您使用Registry.CurrentUser

,则不需要