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
John500Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

silemoneCommented:
place in a try/catch when you debug and see what exception may be occuring...

do something

try
{
         //your try
}
catch(Exception ex{

string x = ex.StackTrace;                        //<---place a breakpoint here or before    
                                                               //       and debug

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
silemoneCommented:
also  you may need some if statements to test if null


if (MyTestRegKey2 != String.Empty)
{
 foreach(string subKeyName2 in MyTestRegKey2.GetSubKeyNames())     //  FAILS HERE
            {
             ...
             ...
}
0
John500Author Commented:
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
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

John500Author Commented:
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...
0
John500Author Commented:
Nope, no difference without the brackets
0
silemoneCommented:
so you see the value, but it's not storing in your variable?
0
John500Author Commented:
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?
0
silemoneCommented:
I would have to write a similar code to know for sure, but I don't see how that could be the case...
0
John500Author Commented:
>> 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

0
John500Author Commented:
In terms of the regular expression, you can try with and without by swaping 'subKeyName1' for 'SubValue1' for the call to OpenSubKey()....
0
John500Author Commented:
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............
0
silemoneCommented:
Just happy you got it working...

cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.