John500
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.GetSubKeyName s())
{
using(RegistryKey tempKey = MyTestRegKey.OpenSubKey(su bKeyName))
{
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.GetSubKeyNam es())
{
RegistryKey MyTestRegKey2 = Microsoft.Win32.Registry.L ocalMachin e.OpenSubK ey(@"SOFTW ARE\...... ....\" + subKeyName1 ,true);
foreach(string subKeyName2 in MyTestRegKey2.GetSubKeyNam es()) // 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
The code below enables me to obtain a subkey and the associated string value or value name.
foreach(string subKeyName in MyTestRegKey.GetSubKeyName
{
using(RegistryKey tempKey = MyTestRegKey.OpenSubKey(su
{
foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}:
tempKey.GetValue(valueName
}
}
}
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.GetSubKeyNam
{
RegistryKey MyTestRegKey2 = Microsoft.Win32.Registry.L
foreach(string subKeyName2 in MyTestRegKey2.GetSubKeyNam
{
...
...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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());
}
}
}
values.jpg
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...
ASKER
Nope, no difference without the brackets
so you see the value, but it's not storing in your variable?
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?
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...
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.
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;
}
}
}
}
ASKER
In terms of the regular expression, you can try with and without by swaping 'subKeyName1' for 'SubValue1' for the call to OpenSubKey()....
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
cheers
if (MyTestRegKey2 != String.Empty)
{
foreach(string subKeyName2 in MyTestRegKey2.GetSubKeyNam
{
...
...
}