开发者

How to use Microsoft.Win32.Registry.OpenSubKey to go directly to a particular key?

开发者 https://www.devze.com 2023-03-29 09:44 出处:网络
Now this is a simple question. It should be clearly documented in MSDN. I looked but I couldn\'t find it. The only thing I got was that I had to open subkey after subkey after subkey to get to the par

Now this is a simple question. It should be clearly documented in MSDN. I looked but I couldn't find it. The only thing I got was that I had to open subkey after subkey after subkey to get to the particular key I'm interested in.

Surely there is a more direct method to access a key 3 levels deep. What is it?

I've already tried

RegistryKey reg = Registry.LocalMachine;
r开发者_运维知识库eg.OpenSubKey(@"Software\Microsoft", true);  // reg is still HKLM !

and

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


I think you are expecting the OpenSubKey() method to do something to reg - somehow to make it point to the sub key. It doesn't work that way. OpenSubKey() returns a new object of type RegistryKey which can be used to retrieve the value of, or modify, the sub key. So you need:

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


OpenSubKey returns a new RegistryKey object :

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


Sure just put your full path of that key, example:

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

精彩评论

暂无评论...
验证码 换一张
取 消