2011-08-18 25 views
3

現在這是一個簡單的問題。它應該清楚地記錄在MSDN中。我看了但我找不到它。我得到的唯一的東西是我必須打開子項後的子項後,才能得到我感興趣的特定密鑰的子項。如何使用Microsoft.Win32.Registry.OpenSubKey直接轉到特定的鍵?

當然,有一種更直接的方法來訪問密鑰3級深。它是什麼?

我已經嘗試過

RegistryKey reg = Registry.LocalMachine; 
reg.OpenSubKey(@"Software\Microsoft", true); // reg is still HKLM ! 

reg.OpenSubKey(@"Software\Microsoft\", true); // reg is still HKLM ! 

回答

8

我想你期待的OpenSubKey()方法去做一些事情reg - 以某種方式使其指向子密鑰。它不這樣工作。 OpenSubKey()返回RegistryKey類型的新對象,該對象可用於檢索或修改子密鑰的值。因此,你需要:

RegistryKey reg = Registry.LocalMachine; 
RegistryKey subKey = reg.OpenSubKey(@"Software\Microsoft", true); 
0

當然只要把你的那把鑰匙,例如完整路徑:

Registry.CurrentUser.OpenSubKey("the registry full path"); 
1

OpenSubKey返回一個新RegistryKey對象:

reg = reg.OpenSubKey(@"Software\Microsoft", true); // Will work or 
var sub = reg.OpenSubKey(@"Software\Microsoft", true); 
相關問題