2016-04-21 7 views
6

Desidero un modello pulito/compatto che sia eccezionalmente sicuro e che disponga correttamente rulesKey, anche se è stato generato qualcosa. Questo non sembra possibile con using (a meno che non sia il 4 using s ma sembra così prolisso e l'apertura di risorse aggiuntive che potrebbe non essere nemmeno necessario aprire). Quello che mi dà è che questo sarebbe così diretto/facile in C++. C'è una buona soluzione?C#: esiste un modello pulito per trovare l'oggetto giusto combinato con l'utilizzo?

{ 
    RegistryKey rulesKey = null; 
    rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Company\\Internal\\Product"); 
    rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Company\\Company\\Internal\\Product"); 
    rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Company\\Product"); 
    rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Company\\Product"); 

    // Code using rulesKey, might throw 

    rulesKey.Close(); 
} 

risposta

8

Si potrebbe utilizzare

using (RegistryKey rulesKey = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Company\\Internal\\Product") 
           ?? Registry.LocalMachine.OpenSubKey("Software\\Company\\Company\\Internal\\Product") 
           ?? Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Company\\Product") 
           ?? Registry.LocalMachine.OpenSubKey("Software\\Company\\Product")) 
{ 
    //use rulesKey here 
} 

Dal

L'istruzione using assicura che Dispose viene chiamato anche se un'eccezione si verifica mentre si sta chiamando metodi sull'oggetto. È possibile ottenere lo stesso risultato inserendo l'oggetto in un blocco try e quindi chiamando Dispose in un blocco finally; in effetti, questo è il modo in cui l'istruzione using viene tradotta dal compilatore. MSDN

+0

E se non fosse possibile aprire le chiavi? –

+1

Poiché ['OpenSubKey'] (https://msdn.microsoft.com/en-us/library/z9f66s0a (v = vs.110) .aspx) restituisce null se fallisce, quindi' rulesKey' sarà null se tutto fallito. – AGB

+1

Se nessuna delle chiavi è aperta, sarà nullo, il che è sicuro per l'istruzione 'using', almeno – VoidStar

0

Si potrebbe anche fare qualcosa di simile al seguente. L'esempio è hard-coded ma potresti facilmente caricare i percorsi da un file o database o altro. Ciò consentirebbe di aggiungere ulteriori chiavi senza gonfiare l'istruzione using. Si indurisce anche contro l'accesso limitato

List<string> keyPaths = GetKeyPaths(); 
private void DoStuff() 
{   
    // key is disposed when leaving using block 
    using(var key = OpenFirstAvailableKey()) 
    { 
     if(key == null) 
     { 
      // handle error 
     } 

     // do stuff 
    } 
} 

private RegistryKey OpenFirstAvailableKey() 
{ 
    RegistryKey result = null; 
    try 
    { 
     foreach(var key in keyPaths) 
     { 
      result = Registry.LocalMachine.OpenSubKey(key); 
      if(result != null) 
      { 
       break; 
      } 
     }  
    } 
    catch (System.Exception) 
    { 
     // handle or throw   
     throw; 
    } 

    return result; 
} 

private List<string> GetKeyPaths() 
{ 
    List<string> paths = new List<string>(); 
    // Load from db or file or whatever...or just hardcode 
    paths.Add("Software\\Wow6432Node\\Company\\Internal\\Product"); 
    paths.Add("Software\\Company\\Company\\Internal\\Product"); 
    paths.Add("Software\\Wow6432Node\\Company\\Product"); 
    paths.Add("Software\\Company\\Product"); 

    return paths; 
} 
Problemi correlati