简体   繁体   English

RegistryKey 更改为新的 csproj 格式

[英]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.对于我们的一个项目,我更改了 csproj 格式,以便为 .net Core 迁移做准备。 Currently we are using .net Framework 4.8.目前我们使用的是 .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.两次应用程序都是在同一台 64 位机器上使用 AnyCPU 在 Debug 中构建的。

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.在更改之前返回 ~400 个键,之后它只返回 ~200。

Why does the result of this method call changed when moving from the old csproj format to the new one?为什么从旧的 csproj 格式转移到新的 csproj 格式时,此方法调用的结果会发生变化?

I can´t tell you why it changed, but it seems that the .net core compilation rules are used with the new csproj format.我不能告诉你它为什么改变,但似乎 .net 核心编译规则与新的 csproj 格式一起使用。 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 .所以这个问题也会发生,当你从 .net Framework 迁移到 .net Core 时,就像这里已经问过的那样搜索注册表项在 .net core 和 .net framwork 中提供不同的输出

Compiled for "x86":为“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":为“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"为“AnyCpu”编译的 .NET 应用程序

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.长话短说,解决方案也是硬编码检查 64 位注册表项。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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