Link to home
Start Free TrialLog in
Avatar of John500
John500Flag for United States of America

asked on

How to obtain a subkey of the registry

Greetings,

The code below enables me to obtain a subkey and the associated string value or value name.


 foreach(string subKeyName in MyTestRegKey.GetSubKeyNames())
        {
            using(RegistryKey tempKey = MyTestRegKey.OpenSubKey(subKeyName))
            {
 
                foreach(string valueName in tempKey.GetValueNames())
                {
                    Console.WriteLine("{0,-8}: {1}", valueName,
                        tempKey.GetValue(valueName).ToString());
                }
            }
        }

However, if I try to get the subkeys of each *unknown*   subKeyName1   I can't do it with this approach:


         foreach(string subKeyName1 in MyTestRegKey1.GetSubKeyNames())
        {

            RegistryKey MyTestRegKey2 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\..........\" + subKeyName1 ,true);

            foreach(string subKeyName2 in MyTestRegKey2.GetSubKeyNames())     //  FAILS HERE
            {
             ...
             ...

When I step through the code during debug, the variable 'MyTestRegKey2' surely reveals a successful return from OpenSubkey.  However, for some reason the call to 'GetSubKeyNames' which follows fails to return the values under this subkey.

When I say *unknown* - I mean that these keys are created dynamically so I can't make a direct reference to them with OpenSubkey.

Is there something obvious I'm overlooking?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of silemone
silemone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
also  you may need some if statements to test if null


if (MyTestRegKey2 != String.Empty)
{
 foreach(string subKeyName2 in MyTestRegKey2.GetSubKeyNames())     //  FAILS HERE
            {
             ...
             ...
}
Avatar of John500

ASKER

silemone,

Appreciate the feed back.  

I did as you suggested but there are no exceptions.  As far as the test for an empty string, MyTestRegKey2.ToString() passes the test as not being empty.  Its value is easily seen in debug mode.  If I use the Console and add a little code I get the values in the picture below.

Any ideas?

 
// Print the information from the MyTestRegKey subkey.
        Console.WriteLine("There are {0} subkeys under {1}.", 
            MyTestRegKey.SubKeyCount.ToString(), MyTestRegKey.Name);
        foreach(string subKeyName in MyTestRegKey.GetSubKeyNames())
        {
            using(RegistryKey 
                tempKey = MyTestRegKey.OpenSubKey(subKeyName))
            {
                Console.WriteLine("\nThere are {0} values for {1}.", 
                    tempKey.ValueCount.ToString(), tempKey.Name);
                foreach(string valueName in tempKey.GetValueNames())
                {
                    Console.WriteLine("{0,-8}: {1}", valueName, 
                        tempKey.GetValue(valueName).ToString());
                }
            }
        }

Open in new window

values.jpg
Avatar of John500

ASKER

I'm thinking it it's because of the curly brackets {....} that GetSubKeyNames() is choking on.  Maybe I need to parse this before passing it...
Avatar of John500

ASKER

Nope, no difference without the brackets
so you see the value, but it's not storing in your variable?
Avatar of John500

ASKER

Yes, I see the value.  Does anything pop out at you that I'm not doing which is done in post:  ID:22876349

The only thing I'm doing different from post 'ID:22876349' is to OpenSubKey() a second time.

Is this a case of code blind - right in front of me/us?
I would have to write a similar code to know for sure, but I don't see how that could be the case...
Avatar of John500

ASKER

>> I would have to write similar code to know for sure

Don't bother writing it, below is the code I'm using.  It's short and simple Console program.

I don't know if you have the same tree as I do - right down to the key, but maybe you could find one if not?

Thanks in advance if you should try.

 

using System;
using Microsoft.Win32;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WriteToFile;
 
 
namespace RegistryCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            string SubValue1 = string.Empty;
            Regex RegExpress = new Regex("{(.*?)}");
            RegistryKey TestRegKey1 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles", true);
 
 
            // Print the information from the TestRegKey1 subkey.
            Console.WriteLine("There are {0} subkeys under {1}.", TestRegKey1.SubKeyCount.ToString(), TestRegKey1.Name);
            foreach (string subKeyName in TestRegKey1.GetSubKeyNames())
            {
                using (RegistryKey
                    tempKey = TestRegKey1.OpenSubKey(subKeyName))
                {
                    Console.WriteLine("\nThere are {0} values for {1}.",
                        tempKey.ValueCount.ToString(), tempKey.Name);
                    foreach (string valueName in tempKey.GetValueNames())
                    {
                        Console.WriteLine("{0,-8}: {1}", valueName,
                            tempKey.GetValue(valueName).ToString());
                    }
                }
            }
 
	  foreach (string subKeyName1 in TestRegKey1.GetSubKeyNames())
           {
               Match m = RegExpress.Match(subKeyName1);
               if (m.Groups.Count > 1)
                   SubValue1 = m.Groups[1].Value;
 
               RegistryKey TestRegKey2 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\" + SubValue1, true);
 
               try
               {
                   if(TestRegKey2.ToString() != String.Empty)
                   
                   foreach (string subKeyName2 in TestRegKey2.GetSubKeyNames())
                   {
                       using (RegistryKey tempKey = TestRegKey2.OpenSubKey(subKeyName2, true))
                       {
                           foreach (string valueName in tempKey.GetValueNames())
                           {
                               Console.WriteLine("{0,-8}: {1}", valueName,
                               TestRegKey2.GetValue(valueName).ToString());
 
                           }
                       }
                   }
               }
               catch (Exception ex)
               {
                   string KeyError = ex.StackTrace;
               }
           }
	}
}

Open in new window

Avatar of John500

ASKER

In terms of the regular expression, you can try with and without by swaping 'subKeyName1' for 'SubValue1' for the call to OpenSubKey()....
Avatar of John500

ASKER

I don't know why I didn't do this sooner but I ended up just using the code that worked in post ID:22876349.  Thanks for listening............
Just happy you got it working...

cheers