2012-12-05 72 views
54

我試圖讓這個鍵中的子鍵的所有顯示名字看:OpenSubKey()返回null註冊表項,我可以在REGEDIT.EXE

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 

有了這個代碼:

 RegistryKey newKey; 
    string val; 

    string KeyPath64Bit = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
    RegistryKey mainKey = Registry.LocalMachine.OpenSubKey(KeyPath64Bit); 

    string[] RegKeys64Bits = Registry.LocalMachine.OpenSubKey(KeyPath64Bit).GetSubKeyNames(); 

    foreach (string s in RegKeys64Bits) 
    { 
     newKey = mainKey.OpenSubKey(s); 
     val = newKey.GetValue("DisplayName", -1, RegistryValueOptions.None).ToString(); 
     if (val != "-1") 
      file64.WriteLine(val); 
    } 

運行代碼後,我無法找到的關鍵之一,我需要:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E} 

它應該具有顯示名稱:Microsoft Visual C++ 2010 x64 Redistributable - 10.0.30319,而是GetSubKeyNames()方法爲我提供了子項:,它沒有任何顯示名稱。

爲什麼我不能得到確切的子鍵,我需要({DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}),我怎樣才能得到它呢?

+0

如果以管理員身份運行Visual Studio,您可以這樣做嗎? – tsells

+0

@tsells嘗試過了,這是行不通的。 –

+0

你在32位進程運行在64位操作系統? –

回答

125

上的64位操作系統的32位應用程序將尋找在默認情況下,HKLM\Software\Wow6432Node節點。讀取密鑰的64位版本,你需要指定RegistryView

using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) 
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")) 
{ 
    // key now points to the 64-bit key 
} 

要做到這一點,加入在.NET 4.0中的API;如果您仍然使用3.5,則需要使用P/Invoke訪問64位密鑰: http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

+0

謝謝,這工作 –

+0

這是工作solution.Thanks救了我的時間。 –

+0

謝謝。我有同樣的問題,使用相同的密鑰。 :) – ECC

相關問題