如何使用Microsoft.Win32.Registry.OpenSubKey直接转到特定键?

现在这是一个简单的问题。 它应该在MSDN中清楚地记录。 我看了,但我找不到它。 我唯一得到的是我必须在subkey之后打开子键才能找到我感兴趣的特定键。

当然,有一种更直接的方法来访问关键3级深度。 它是什么?

我已经试过了

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

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

我认为你期望OpenSubKey()方法做一些事情来reg – 以某种方式使它指向子键。 它不起作用。 OpenSubKey()返回一个RegistryKey类型的新对象,可用于检索子键的值或修改子键。 所以你需要:

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

OpenSubKey返回一个新的RegistryKey对象:

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

当然只是把你的完整路径,例如:

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