2012-12-05 5 views
54

Sto cercando di ottenere tutti i nomi visualizzati dei tasti secondarie all'interno di questa chiave:OpenSubKey() restituisce null per una chiave di registro che posso vedere in regedit.exe

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 

Con questo codice:

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

Dopo aver eseguito il codice non riesco a trovare una delle chiavi che ho bisogno:

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

E dovrebbe avere il nome visualizzato: Microsoft Visual C++ 2010 x64 Redistributable - 10.0.30319, ma invece il metodo GetSubKeyNames() mi fornisce la sottochiave: che non ha alcun nome visualizzato.

Perché non è possibile ottenere la chiave secondaria esatta di cui ho bisogno ({DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}) e come posso ottenerla?

+0

Puoi farlo se esegui Visual Studio come amministratore? – tsells

+0

@tsells Provato e non funziona. –

+0

Sei in esecuzione in un processo a 32 bit su un sistema operativo a 64 bit? –

risposta

125

Un'applicazione a 32 bit su un sistema operativo a 64 bit esaminerà il nodo HKLM\Software\Wow6432Node per impostazione predefinita. Per leggere la versione a 64 bit della chiave, è necessario specificare il 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 
} 

L'API per fare questo è stata aggiunta in .NET 4.0; se stai ancora utilizzando 3.5, dovrai utilizzare P/Invoke per accedere alle chiavi a 64 bit: http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

+0

Grazie, ha funzionato –

+0

Questa è una soluzione funzionante. Grazie per risparmiare tempo. –

+0

Grazie. Ho avuto lo stesso problema, con la stessa chiave. :) – ECC