我正在尝试读取注册表项下的字符串"连接"的值
HKEY_Local_Machine\软件\投石机\服务器设置\业务流程 服务
在 VB.NET 中,我尝试使用以下代码读取此密钥:
Private Function ReadRegistry()
Dim KeyValue As String = ""
Dim regkey = Registry.LocalMachine.OpenSubKey("SOFTWARETrebuchetServerSetupBusiness Process Service", False)
If regkey IsNot Nothing Then KeyValue = CStr(regkey.GetValue("Connection"))
Return KeyValue
End Function
但是,在尝试检查注册表时,我收到 regkey 的空值。我已经验证了该值是否在该键内,甚至将 OpenSubKey 调用中的文本替换为从 RegEdit 检索的键名称的精确副本,但似乎 VB 应用程序由于某种原因无法读取它。
我错过了什么吗?
我的猜测是你正在64位操作系统上开发32位应用程序。在这种情况下,Registry
类的共享(在 C# 中是静态的)成员(如 LocalMachine
)将不适合,因为它们正在查找 32 位版本的注册表。您需要在注册表中打开基项,明确指定需要 64 位版本。因此,您的代码可能如下所示:
Private Function ReadRegistry()
Dim KeyValue As String = ""
Dim baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
Dim regkey = baseKey.OpenSubKey("SOFTWARETrebuchetServerSetupBusiness Process Service")
If regkey IsNot Nothing Then KeyValue = CStr(regkey.GetValue("CLSID"))
Return KeyValue
End Function