2012-08-04 105 views
26

我想插入使用C#中Microsoft.Win32.RegistryKey一些簡單的註冊表項,但路徑自動改變自:避免註冊Wow6432Node重定向

HKEY_LOCAL_MACHINE\SOFTWARE\Test 

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test 

我想谷歌但我只能得到一些模糊和令人困惑的結果。有沒有人處理過這個問題?一些示例代碼會有很大的提升。

+1

的[Visual Studio 2010中安裝項目的64位創建註冊表項問題]可能重複( http://stackoverflow.com/questions/7384270/visual-studio-2010-setup-project-64-bit-create-registry-key-issue) – 2012-08-04 15:15:16

回答

24

您可以使用RegistryKey.OpenBaseKey來解決這個問題:

var baseReg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); 
var reg = baseReg.CreateSubKey("Software\\Test"); 
+5

僅在.NET 4+中 – Kevin 2015-03-04 17:36:15

+0

我正在尋找一個如何使用RegistryView.Registry64的例子,偶然在這裏找到它。沒有一個M $ docs有一個例子,他們只是說使用它。當我讓它讀取常規卸載時,我的任何CPU應用程序正在讀取wow6432uninstall。這是解決方案 – sdjuan 2016-10-13 02:46:11

+2

不要忘記配置baseReg! using(var baseReg = ...){..} – 2017-09-01 09:57:17

7

在WOW64下,某些註冊表項被重定向(SOFTWARE)。當32位或64位應用程序針對重定向的鍵進行註冊表調用時,註冊表重定向程序會攔截該調用並將其映射到鍵的相應物理註冊表位置。有關更多信息,請參見Registry Redirector

您可以使用RegistryKey.OpenBaseKey Method上的RegistryView Enumeration明確打開32位視圖並直接訪問HKLM \ Software \。

+0

只能在.net 4+框架中使用.net 3.5應用程序? – Kevin 2015-03-04 17:37:21

+2

@Kevin在.net 4之前,您必須調用本地API [RegOpenKeyEx](https://msdn.microsoft.com/en-us/library/ms724897(VS.85).aspx)傳遞[KEY_WOW64_32KEY或KEY_WOW64_64KEY](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v = vs.85).aspx)標誌。 – 2015-03-05 06:59:16

10

我不知道如何使用.reg文件解決它。但是隻能在BAT文件中使用,如下所示:

您必須在命令行末尾添加/reg:64。 例如:

REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background" /v "OEMBackground" /t REG_DWORD /d 0x00000001 /f /reg:64 

來源:Wow6432Node and how to Deploy Registry settings to 64 bit systems via Sccm

+0

不錯!正是我想要的,謝謝。 – Ron 2015-04-13 00:36:29

0

這裏是我已經發展到雙方工作的代碼讀取和寫入只32位註冊表。它適用於32位和64位應用程序。如果未設置該值,則「讀取」調用會更新註冊表,但非常明顯的是如何刪除該值。它需要.Net 4.0,並使用OpenBaseKey/OpenSubKey方法。

我目前使用它來允許64位後臺服務和32位托盤應用程序無縫地訪問相同的註冊表項。

using Microsoft.Win32; 

namespace SimpleSettings 
{ 
public class Settings 
{ 
    private static string RegistrySubKey = @"SOFTWARE\BlahCompany\BlahApp"; 

    public static void write(string setting, string value) 
    { 
     using (RegistryKey registryView = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)) 
     using (RegistryKey registryCreate = registryView.CreateSubKey(RegistrySubKey)) 
     using (RegistryKey registryKey = registryView.OpenSubKey(RegistrySubKey, true)) 
     { 
      registryKey.SetValue(setting, value, RegistryValueKind.String); 
     }   
    } 
    public static string read(string setting, string def) 
    { 
     string output = string.Empty; 
     using (RegistryKey registryView = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)) 
     using (RegistryKey registryCreate = registryView.CreateSubKey(RegistrySubKey)) 
     using (RegistryKey registryKey = registryView.OpenSubKey(RegistrySubKey, false)) 
     { 
      // Read the registry, but if it is blank, update the registry and return the default. 
      output = (string)registryKey.GetValue(setting, string.Empty); 
      if (string.IsNullOrWhiteSpace(output)) 
      { 
       output = def; 
       write(setting, def); 
      } 
     } 
     return output; 
    } 
} 
} 

用法: 將這個在它自己的類文件(的.cs),並把它作爲這樣的:

using SimpleSettings; 
string mysetting = Settings.read("SETTINGNAME","DEFAULTVALUE");