簡體   English   中英

RegistryKey 更改為新的 csproj 格式

[英]RegistryKey changed with new csproj Format

對於我們的一個項目,我更改了 csproj 格式,以便為 .net Core 遷移做准備。 目前我們使用的是 .net Framework 4.8。

我沒有更改代碼中的任何內容,但后來發生了錯誤,應用程序無法再找到注冊表項。 兩次應用程序都是在同一台 64 位機器上使用 AnyCPU 在 Debug 中構建的。

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

在更改之前返回 ~400 個鍵,之后它只返回 ~200。

為什么從舊的 csproj 格式轉移到新的 csproj 格式時,此方法調用的結果會發生變化?

我不能告訴你它為什么改變,但似乎 .net 核心編譯規則與新的 csproj 格式一起使用。 所以這個問題也會發生,當你從 .net Framework 遷移到 .net Core 時,就像這里已經問過的那樣搜索注冊表項在 .net core 和 .net framwork 中提供不同的輸出

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM