2010-07-24 10 views
9

Quindi nel mio registro ho la voce in "LocalMachine \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run \" chiamata "COMODO Internet Security" che è il mio firewall . Ora quello che mi piacerebbe sapere è come posso ottenere il registro per verificare se quella voce esiste? Se lo fa, se non lo fa, fallo. So come verificare se la sottochiave "Esegui" esiste ma non la voce per "COMODO Internet Security", questo è il codice che stavo usando per ottenere se esiste la sottochiave.Ottieni se la voce del Registro di sistema esiste se così si fa, se così non fosse

   using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\")) 
       if (Key != null) 
       { 

        MessageBox.Show("found"); 
       } 
       else 
       { 
        MessageBox.Show("not found"); 
       } 

risposta

9

Se siete alla ricerca di un valore sotto una sottochiave, (è che cosa si intende per "entry"?) È possibile utilizzare RegistryKey.GetValue(string). Ciò restituirà il valore se esiste e null se non lo è.

Ad esempio:

using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\")) 
    if (Key != null) 
    {  
     string val = Key.GetValue("COMODO Internet Security"); 
     if (val == null) 
     { 
      MessageBox.Show("value not found"); 
     } 
     else 
     { 
      // use the value 
     } 
    } 
    else 
    { 
     MessageBox.Show("key not found"); 
    } 
+0

ok e come faccio a vederlo in localmachine con getvalue? – NightsEVil

+0

Aggiunta di esempio. – jwismar

+0

Errore Impossibile convertire implicitamente il tipo "oggetto" in "stringa". Esiste una conversione esplicita (ti manca un cast?) – NightsEVil

0

Il seguente collegamento dovrebbe chiarire questo: codice

How to check if a registry key/subkey already exists

Esempio:

using Microsoft.Win32; 

RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Geekpedia\\Test"); 

if(rk != null) 
{ 
    // It's there 
} 
else 
{ 
    // It's not there 
} 
+0

ma ho bisogno di cercare una specifica voce di avvio nella macchina locale utente corrente microsoft windows Esegui – NightsEVil

+0

@Leniel: FYI: Se, ad esempio, 'Geekpedia' non è nel Registro di sistema sotto HLKM \ Software, VS2010 lancia un Null Reference Exception quando si tenta di aprire la chiave '" Software \\ Geekpedia \\ Test "'. – jp2code

1

Prova questo:

using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\COMODO Internet Security")) 
{ 
    if (Key != null) 
    MessageBox.Show("found"); 
    else 
    MessageBox.Show("not found"); 
} 
+0

ma ho bisogno di cercare una specifica voce di avvio sotto l'utente corrente della macchina locale Microsoft Windows Esegui – NightsEVil

0

Mi sono imbattuto in un problema di recente in cui stavo cercando di afferrare le sottochiavi in ​​una voce di registro, ma il problema era che poiché stavo iterando su ogni chiave di registro in quella sezione del registro, a volte i valori non avrebbero la sottochiave I stava cercando, e vorrei ottenere un'eccezione di riferimento null quando si tenta di valutare il valore della sottochiave.

Quindi, molto simile a quello che alcune altre risposte fornite, questo è quello che ho finito per andare con:

string subkeyValue = null; 

var subKeyCheck = subkey.GetValue("SubKeyName"); 

if(subKeyCheck != null) 
{ 
    subkeyValue = subkey.GetValue("SubKeyName").ToString(); 
} 

Quindi, a seconda di ciò che il valore che stai cercando sottochiave, semplicemente scambiarlo per "SubKeyName "e questo dovrebbe fare il trucco.

0

Il mio codice

 private void button2_Click(object sender, EventArgs e) 

    { 
     string HKCUval = textBox1.Text; 
     RegistryKey HKCU = Registry.CurrentUser; 
     //Checks if HKCUval exist. 
     try { 
      HKCU.DeleteSubKey(HKCUval); //if exist. 
     } 
     catch (Exception) 
     { 
      MessageBox.Show(HKCUval + " Does not exist"); //if does not exist. 
     } 

     } 

Speranza che aiuta.

Problemi correlati