get RegistryKey's parent?

A

Arthur Dent

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.
 
N

Nick Hertl

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;
}
}
 
A

Arthur Dent

Nick Hertl said:
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;
}
}
 
A

Arthur Dent

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.
 
T

Tiberiu Covaci

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
 
A

Arthur Dent

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top