Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

registry

msdn

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;
reg.OpenSubKey(@"Software\Microsoft", true);  // reg is still HKLM !

and

reg.OpenSubKey(@"Software\Microsoft\", true); // reg is still HKLM !
like image 319
Sam Avatar asked Dec 12 '22 10:12

Sam


1 Answers

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);  
like image 162
Richard Cox Avatar answered May 11 '23 22:05

Richard Cox