简体   繁体   中英

RegistryKey changed with new csproj Format

For one of our projects I changed the csproj Format in order to prepare for the .net Core migration. Currently we are using .net Framework 4.8.

I did not changed anything in the code but afterwards a bug occurred and the application was not capable of finding a registry key anymore. Both times the application was build in Debug with AnyCPU on the same 64 bit machine.

string registry_key32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
    var subKeys = key.GetSubKeyNames();
}

This return ~400 keys before the change, afterwards it only returned ~200.

Why does the result of this method call changed when moving from the old csproj format to the new one?

I can´t tell you why it changed, but it seems that the .net core compilation rules are used with the new csproj format. So this problem will also occur, when you migrate from .net Framework to .net Core like it was already ask in here Searching registry keys giving different outputs in .net core and .net framwork .

Compiled for "x86":

Always 32-bit
On 32-bit platforms, accesses 32-bit registry
On 64-bit platforms, accesses 32-bit registry (inside Wow6432Node)

Compiled for "x64":

Always 64 bit
On 32-bit platforms, won't run
On 64-bit platforms, accesses 64-bit registry (not inside Wow6432Node)

.NET app compiled for "AnyCpu"

Either 32 or 64 bit depending on platform
On 32-bit platforms, accesses 32-bit registry
On 64-bit platforms, accesses 64-bit registry (not inside Wow6432Node)

Long story short, the solution was to also hard code checking the 64bit registry keys.

string registry_key32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
    var subKeys = key.GetSubKeyNames();
}

string registry_key64 = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
    var subKeys = key.GetSubKeyNames();
}

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