OpenSubKey不工作的注册表值,我需要 [英] OpenSubKey not working for Registry value I need

查看:521
本文介绍了OpenSubKey不工作的注册表值,我需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了SQL Server。



在注册表中,关键的的MSSQLServer 是在* HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft *看起来像这样的:



所有的代码返回值从注册表中以下行:

  VAR mainKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey (软件)OpenSubKey(微软)OpenSubKey(MSSQLSERVER:)。 
VAR subKey1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(软件)OpenSubKey(微软)OpenSubKey(MSSQLSERVER:)OpenSubKey(客户)。;
VAR subKey2 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(软件)OpenSubKey(微软)OpenSubKey(MSSQLSERVER:)OpenSubKey(MSSQLSERVER:)OpenSubKey(CURRENTVERSION)。;



<$:



然而,这一个没有p $ p> VAR subKey3 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(软件)OpenSubKey(微软)OpenSubKey(MSSQLSERVER:)OpenSubKey(设置)。;



设置看起来是相同类型的值作为其他键。任何思考为什么这个命令返回null?


解决方案

您的应用程序是32位的,这是一个64位的注册表项。 32位和64位应用程序具有注册表的不同意见。在注册表编辑器,你在代码中所得到的关键是HKLM\Software\Wow6432Node\MSSQLServer,不HKLM\Software\MSSQLServer(在画面中)。



不幸的是,管理 API 访问注册表项不允许32位应用程序获得。注册表的64位视图



有两种选择:更改您的应用程序的目标值为anycpu或x64或的P / Invoke 的非托管API。 有如何做在P为例/ Invoke并 MSDN文档



顺便说一句,你可以传递给OpenSubKey方法,而不是链接在一起这样的路径。



更新:我是不太正确的上方。作为.NET 4中, OpenBaseKey 法(的 MSDN )可以被用来获取一个32位应用程序在64位视图。


I have SQL Server installed.

In the registry, the key MSSQLServer at *HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft* looks like this:

All of the following lines of code return values from the registry:

    var mainKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer");
    var subKey1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer").OpenSubKey("Client");
    var subKey2 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer").OpenSubKey("MSSQLServer").OpenSubKey("CurrentVersion");

However, this one does not:

var subKey3 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSSQLServer").OpenSubKey("Setup");

"Setup" looks to be the same type of value as the other keys. Any thoughts why this command returns null?

解决方案

Your application is 32-bit and that is a 64-bit registry key. 32-bit and 64-bit applications have different views of the registry. In regedit the key you are getting in code is at HKLM\Software\Wow6432Node\MSSQLServer, not HKLM\Software\MSSQLServer (in your picture).

Unfortunately, the managed API for accessing registry keys does not allow a 32-bit application to get a 64-bit view of the registry.

There are two options: Change your application to target AnyCPU or x64 or P/Invoke the unmanaged API. This has an example of how to do the P/Invoke and the MSDN documentation.

BTW, you can pass a path to the OpenSubKey method instead of chaining them together like that.

Update: I was not quite correct above. As of .NET 4, the OpenBaseKey method (MSDN) can be used to get a 64-bit view in a 32-bit application.

这篇关于OpenSubKey不工作的注册表值,我需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆