[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

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
0
John500
Asked:
John500
  • 7
  • 5
1 Solution
 
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
 
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now