简体   繁体   中英

Registry redirection does not happen for HKEY_CURRENT_USER

I have the following code:

RegistryKey objRegKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\MySettings\\Dwm\\Preferences", true);

I have a registry key present under: HKEY_CURRENT_USER\\SOFTWARE\\MySettings\\Dwm\\Preferences and so this works fine.

When I change the above code to read from local machine instead of current user:

RegistryKey objRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySettings\\Dwm\\Preferences", true);

and create the following registry under: HKEY_LOCAL_MACHINE\\SOFTWARE\\MySettings\\Dwm\\Preferences

The code returns null. I understand the reason being that due to registry redirection for 32 bit applications on a 64 bit OS, it tries to read the registry from: HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432NODE\\MySettings\\Dwm\\Preferences and since the registry does not exists, it returns null. The question is that why redirection only happens for HKEY_LOCAL_MACHINE and not for HKEY_CURRENT_USER ?

Registry Redirection isn't a binary option - either present or not. Sometimes you want both 32-bit and 64-bit applications to have the same "view" of (part of) the registry and in other cases you do not.

That's why the specific keys affected by redirection are documented. HKEY_CURRENT_USER itself is shared, as is HKEY_CURRENT_USER\\Software . However, HKEY_CURRENT_USER\\Software\\Classes\\CLSID , which includes COM component registration data (which is necessarily 32/64 bit split) does perform redirection.

Or, to put it another way, if registry redirection was universal, you wouldn't need all of this convolution involving Wow6432Node , etc. You'd just have a 32-bit registry and a 64-bit registry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM