Mario The Spoon
Mario The Spoon

Reputation: 4859

What is the difference in the registry and registry hives

When I run the following command, rKey has two values.

RegistryKey sqlServer = Registry.LocalMachine.OpenSubKey( @"SOFTWARE\Microsoft\Microsoft SQL Server", false);

When I run either of the following commands (on the same machine as the same user) I find no values;

 RegistryKey sqlServer64 = RegistryKey.OpenBaseKey( RegistryHive.LocalMachine, RegistryView.Registry64);

   RegistryKey sqlServer32 = RegistryKey.OpenBaseKey( RegistryHive.LocalMachine, RegistryView.Registry32 );

Can anyone point me to the answer or a description of the hives vs plain registry access?

Edit: What I do afterwards is :

 StringBuilder sbKeys = new StringBuilder();

  foreach (var key in sqlServer.GetValueNames() )
  {
       sbKeys.AppendLine( key );
  }

For all RegistryKeys. For sqlServer I see two Values, for sqlServer32 and SqlServer64 there are no values.

Upvotes: 2

Views: 947

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

The problem in your second variant is that you have failed to open a sub key. Your sqlServer.GetValueNames() call operates at the root level of a particular hive.

You need it to be like this:

RegistryKey root = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey sqlServer = root.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
foreach (var key in sqlServer.GetValueNames())
{ .... }

Done this way it's no different from your first variant (apart from the registry view). I expect that using the appropriate registry view will lead to the solution to your other question.

Naturally you'll want to add some error checking to the code above.

Upvotes: 1

Related Questions