OpenSubKey under HKLM\Software returning null

£可爱£侵袭症+ 提交于 2020-01-03 08:15:18

问题


Here's my code:

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ADM");

The registry entry exists on the machine. key is always null.

I don't think that this is a security issue. I'm running as Administrator. (I've even explicitly ran the assembly under Administrator mode).

I'm using Visual Studio 2010 running on Windows 7 64bit.


回答1:


The problem is that I'm running 64bit and my app is compiled as 32bit.

The key being read by:

Microsoft.Win32.RegistryKey key = 
    Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE");

Is not HKLM\SOFTWARE but instead HKLM\SOFTWARE\Wow6432Node\. Compiling the application as x64 solves the problem.




回答2:


Try opening each registry key individually like this

Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE");
Microsoft.Win32.RegistryKey key2 = key1.OpenSubKey(@"ADM");

Instead of using the string @"SOFTWARE\ADM";




回答3:


I've run your code with a different application name (one I have a key for) and everything was ok, so the code is ok, but I tested this on Win XP.

When studying writing to the registry, I found this article about registry virtualization in Windows 7 that might cause your problems:

Windows Vista and later versions of Windows improve application compatibility for these applications by automatically redirecting these operations. For example, registry operations to the global store (HKEY_LOCAL_MACHINE\Software) are redirected to a per-user location within the user's profile known as the virtual store (HKEY_USERS\_Classes\VirtualStore\Machine\Software).



来源:https://stackoverflow.com/questions/3655968/opensubkey-under-hklm-software-returning-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!