Hi all, im wondering if theres any way to get the parent of a RegistryKey?
Using the Microsoft.Win32.RegistryKey class?

I am using the Application.UserAppDataRegistry object. I want to then
get the parent key of this key.

The reason for this is that the key returned by UserAppDataRegistry is
specific to the application version, which i dont want, i only want a key
which is specific to the application, ie, the parent of the version key.
Instead of

HKEY_CURRENT_USER\Software\My Company\MyApp\1.0.2095.32456

i just want

HKEY_CURRENT_USER\Software\My Company\MyApp

Thanks in advance,
- Arthur Dent.

Re: get RegistryKey's parent? by Nick

Nick
Mon Sep 26 19:45:25 CDT 2005

I'm not sure if this is exposed by the Win32 APIs even, but here is a
little C# app that does it for you:

using System;
using Microsoft.Win32;

class MainClass
{
public static void Main()
{
string mainkeyname = @"SOFTWARE\Microsoft";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(mainkeyname);
if(rk != null)
{
try
{
RegistryKey parent = GetParent(rk);
try
{
Console.WriteLine("Got it: {0}", parent.Name);
// do stuff here
}
finally
{
parent.Close();
}
}
finally
{
rk.Close();
}
}
}

private static RegistryKey GetParent(RegistryKey value)
{
int first = value.Name.IndexOf(@"\");
int last = value.Name.LastIndexOf(@"\");
string parentname = value.Name.Substring(first+1, last-first);
RegistryKey parent = Registry.LocalMachine.OpenSubKey(parentname);
if(parent != null)
{
return parent;
}
return null;
}
}


Re: get RegistryKey's parent? by Arthur

Arthur
Mon Sep 26 19:53:22 CDT 2005


"Nick Hertl" <nhertl@gmail.com> wrote in message
news:1127781925.690450.47900@z14g2000cwz.googlegroups.com...
> I'm not sure if this is exposed by the Win32 APIs even, but here is a
> little C# app that does it for you:
>
> using System;
> using Microsoft.Win32;
>
> class MainClass
> {
> public static void Main()
> {
> string mainkeyname = @"SOFTWARE\Microsoft";
> RegistryKey rk = Registry.LocalMachine.OpenSubKey(mainkeyname);
> if(rk != null)
> {
> try
> {
> RegistryKey parent = GetParent(rk);
> try
> {
> Console.WriteLine("Got it: {0}", parent.Name);
> // do stuff here
> }
> finally
> {
> parent.Close();
> }
> }
> finally
> {
> rk.Close();
> }
> }
> }
>
> private static RegistryKey GetParent(RegistryKey value)
> {
> int first = value.Name.IndexOf(@"\");
> int last = value.Name.LastIndexOf(@"\");
> string parentname = value.Name.Substring(first+1, last-first);
> RegistryKey parent = Registry.LocalMachine.OpenSubKey(parentname);
> if(parent != null)
> {
> return parent;
> }
> return null;
> }
> }
>



Re: get RegistryKey's parent? by Arthur

Arthur
Mon Sep 26 20:02:33 CDT 2005

I think i just sent an empty reply ... apologies.

Thanks for the ideas and info.

That would only work for HKLM though, no?
The app data info is under HKCU, and it would
definitely be preferable to have something that is
not tied to any particular hive.

I think i should be able to work out something
for my immediate needs though along these lines.

Thanks.

"Nick Hertl" <nhertl@gmail.com> wrote in message
news:1127781925.690450.47900@z14g2000cwz.googlegroups.com...
> I'm not sure if this is exposed by the Win32 APIs even, but here is a
> little C# app that does it for you:
>
> using System;
> using Microsoft.Win32;
>
> class MainClass
> {
> public static void Main()
> {
> string mainkeyname = @"SOFTWARE\Microsoft";
> RegistryKey rk = Registry.LocalMachine.OpenSubKey(mainkeyname);
> if(rk != null)
> {
> try
> {
> RegistryKey parent = GetParent(rk);
> try
> {
> Console.WriteLine("Got it: {0}", parent.Name);
> // do stuff here
> }
> finally
> {
> parent.Close();
> }
> }
> finally
> {
> rk.Close();
> }
> }
> }
>
> private static RegistryKey GetParent(RegistryKey value)
> {
> int first = value.Name.IndexOf(@"\");
> int last = value.Name.LastIndexOf(@"\");
> string parentname = value.Name.Substring(first+1, last-first);
> RegistryKey parent = Registry.LocalMachine.OpenSubKey(parentname);
> if(parent != null)
> {
> return parent;
> }
> return null;
> }
> }
>



Re: get RegistryKey's parent? by Tiberiu

Tiberiu
Tue Sep 27 02:07:55 CDT 2005

Hi Arthur,

Beside LocalMachine you, have several other members in the
Microsoft.Win32.Registry class, which will help you solve your problem... If
you are considering creating a new application, I advice you though not to
use registry, but configuration files, as registry is considered "obsolete",
and only Win32 has it, and will make your application NOT "platform
independent", even though the only platform that .NET is available for is
Win32.

Tibi


"Arthur Dent" <hitchhikersguideto-news@yahoo.com> wrote in message
news:O1R729vwFHA.3856@tk2msftngp13.phx.gbl...
>I think i just sent an empty reply ... apologies.
>
> Thanks for the ideas and info.
>
> That would only work for HKLM though, no?
> The app data info is under HKCU, and it would
> definitely be preferable to have something that is
> not tied to any particular hive.
>
> I think i should be able to work out something
> for my immediate needs though along these lines.
>
> Thanks.
>
> "Nick Hertl" <nhertl@gmail.com> wrote in message
> news:1127781925.690450.47900@z14g2000cwz.googlegroups.com...
>> I'm not sure if this is exposed by the Win32 APIs even, but here is a
>> little C# app that does it for you:
>>
>> using System;
>> using Microsoft.Win32;
>>
>> class MainClass
>> {
>> public static void Main()
>> {
>> string mainkeyname = @"SOFTWARE\Microsoft";
>> RegistryKey rk = Registry.LocalMachine.OpenSubKey(mainkeyname);
>> if(rk != null)
>> {
>> try
>> {
>> RegistryKey parent = GetParent(rk);
>> try
>> {
>> Console.WriteLine("Got it: {0}", parent.Name);
>> // do stuff here
>> }
>> finally
>> {
>> parent.Close();
>> }
>> }
>> finally
>> {
>> rk.Close();
>> }
>> }
>> }
>>
>> private static RegistryKey GetParent(RegistryKey value)
>> {
>> int first = value.Name.IndexOf(@"\");
>> int last = value.Name.LastIndexOf(@"\");
>> string parentname = value.Name.Substring(first+1, last-first);
>> RegistryKey parent = Registry.LocalMachine.OpenSubKey(parentname);
>> if(parent != null)
>> {
>> return parent;
>> }
>> return null;
>> }
>> }
>>
>
>



Re: get RegistryKey's parent? by Arthur

Arthur
Tue Sep 27 10:15:00 CDT 2005

Hi, thanks for the reminder about config files.
Its funny, I usually write ASP.NET app, and
am really writing my first significant Win app
since moving to the .NET platform.
I use .configs all the time in ASP, but completely
forgot about them for Windows.

Thanks for the "duh" reminder!


"Tiberiu Covaci" <tibi@ekoturdata.se> wrote in message
news:e3ZVkHzwFHA.2656@TK2MSFTNGP09.phx.gbl...
> Hi Arthur,
>
> Beside LocalMachine you, have several other members in the
> Microsoft.Win32.Registry class, which will help you solve your problem...
> If you are considering creating a new application, I advice you though not
> to use registry, but configuration files, as registry is considered
> "obsolete", and only Win32 has it, and will make your application NOT
> "platform independent", even though the only platform that .NET is
> available for is Win32.
>
> Tibi
>
>
> "Arthur Dent" <hitchhikersguideto-news@yahoo.com> wrote in message
> news:O1R729vwFHA.3856@tk2msftngp13.phx.gbl...
>>I think i just sent an empty reply ... apologies.
>>
>> Thanks for the ideas and info.
>>
>> That would only work for HKLM though, no?
>> The app data info is under HKCU, and it would
>> definitely be preferable to have something that is
>> not tied to any particular hive.
>>
>> I think i should be able to work out something
>> for my immediate needs though along these lines.
>>
>> Thanks.
>>
>> "Nick Hertl" <nhertl@gmail.com> wrote in message
>> news:1127781925.690450.47900@z14g2000cwz.googlegroups.com...
>>> I'm not sure if this is exposed by the Win32 APIs even, but here is a
>>> little C# app that does it for you:
>>>
>>> using System;
>>> using Microsoft.Win32;
>>>
>>> class MainClass
>>> {
>>> public static void Main()
>>> {
>>> string mainkeyname = @"SOFTWARE\Microsoft";
>>> RegistryKey rk = Registry.LocalMachine.OpenSubKey(mainkeyname);
>>> if(rk != null)
>>> {
>>> try
>>> {
>>> RegistryKey parent = GetParent(rk);
>>> try
>>> {
>>> Console.WriteLine("Got it: {0}", parent.Name);
>>> // do stuff here
>>> }
>>> finally
>>> {
>>> parent.Close();
>>> }
>>> }
>>> finally
>>> {
>>> rk.Close();
>>> }
>>> }
>>> }
>>>
>>> private static RegistryKey GetParent(RegistryKey value)
>>> {
>>> int first = value.Name.IndexOf(@"\");
>>> int last = value.Name.LastIndexOf(@"\");
>>> string parentname = value.Name.Substring(first+1, last-first);
>>> RegistryKey parent = Registry.LocalMachine.OpenSubKey(parentname);
>>> if(parent != null)
>>> {
>>> return parent;
>>> }
>>> return null;
>>> }
>>> }
>>>
>>
>>
>
>