c# - RegistryKey 更改为新的 csproj 格式
问题描述
对于我们的一个项目,我更改了 csproj 格式,以便为 .net Core 迁移做准备。目前我们使用的是 .net Framework 4.8。
我没有更改代码中的任何内容,但后来发生了错误,应用程序无法再找到注册表项。两次应用程序都是在同一台 64 位机器上使用 AnyCPU 调试构建的。
string registry_key32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
var subKeys = key.GetSubKeyNames();
}
这会在更改前返回约 400 个键,之后仅返回约 200 个。
为什么从旧的 csproj 格式转移到新的格式时,此方法调用的结果会发生变化?
解决方案
我不能告诉你它为什么会改变,但似乎 .net 核心编译规则与新的 csproj 格式一起使用。因此,当您从 .net Framework 迁移到 .net Core 时,也会出现此问题,就像在此处搜索注册表项在 .net core 和 .net 框架中给出不同的输出一样。
为“x86”编译:
Always 32-bit
On 32-bit platforms, accesses 32-bit registry
On 64-bit platforms, accesses 32-bit registry (inside Wow6432Node)
为“x64”编译:
Always 64 bit
On 32-bit platforms, won't run
On 64-bit platforms, accesses 64-bit registry (not inside Wow6432Node)
为“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)
长话短说,解决方案也是硬编码检查 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();
}
推荐阅读
- json - 解析没有键名的 JSON 以检索列
- javascript - 将具有数组值的对象键转换为查询字符串
- laravel - 已解决 Laravel 网站对 GET 请求返回“错误 404”
- reactjs - 当 react-router-dom 链接与材料 ui ListItem onClick 一起使用时,反应状态未更新
- python - Pandas Groupby 剧情布局
- python - 在 matplotlib 中创建动态脚注文本
- node.js - NodeJs项目如何使用户输入未经处理?
- angular - 单元测试角度和茉莉花
- flutter - 在 Flutter 中自动为用户移动可拖动对象到拖动目标
- javascript - 我有一些简单的 JavaScript 代码,我想把它变成一个弹出扩展。我该怎么做呢?