无法读取注册表项的值 - VB.NET - HKLM

时间:2016-06-14 04:20:33

标签: vb.net registry

我试图在注册表项下读取字符串的值' Connection'

  

HKEY_Local_Machine \ Software \ Trebuchet \ ServerSetup \ Business Process   服务

在VB.NET中,我尝试使用以下代码读取此密钥:

Private Function ReadRegistry()
    Dim KeyValue As String = ""
    Dim regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Trebuchet\ServerSetup\Business Process Service", False)
    If regkey IsNot Nothing Then KeyValue = CStr(regkey.GetValue("Connection"))

   Return KeyValue
End Function

但是,在尝试检查注册表时,我收到了regkey的null。我已经验证了该值在该键内,并且甚至将OpenSubKey调用中的文本替换为从RegEdit检索到的键名的精确副本,但似乎VB应用程序无法读取它出于某种原因。

我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

我的猜测是你在64位操作系统上开发32位应用程序。在这种情况下,像Registry这样的LocalMachine类的共享(C#中的静态)成员不合适,因为他们正在寻找32位版本的注册表。 您需要在注册表中打开基本密钥,明确指定您需要64位版本。所以你的代码可能如下所示:

Private Function ReadRegistry()
    Dim KeyValue As String = ""
    Dim baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    Dim regkey = baseKey.OpenSubKey("SOFTWARE\Trebuchet\ServerSetup\Business Process Service")
    If regkey IsNot Nothing Then KeyValue = CStr(regkey.GetValue("CLSID"))

    Return KeyValue
End Function
相关问题