Ian
Ian

Reputation: 5725

The most simple way to get the value of a registry key

Is it possible to get a value of a "registry key" without parsing the path to the registry?

What I am actually looking for is a one liner command to get the value of a key like:

object value = Registry.GetValue("path");

The "path", for example, is: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\Install

The Install key is actually the value.

Currently, I am doing this:

object value = Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full", "Install", null);

So question, is it possible to get the value of a registry key without parsing the path?

Thanks!

Upvotes: 1

Views: 8640

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292365

You don't need to parse the key name and value name yourself, the Path class can do it for you:

static object GetRegistryValue(string fullPath, object defaultValue)
{
    string keyName = Path.GetDirectoryName(fullPath);
    string valueName = Path.GetFileName(fullPath);
    return Registry.GetValue(keyName, valueName, defaultValue);
}

Upvotes: 8

Shoban
Shoban

Reputation: 23016

You can use Registry.LocalMachine for HKLM. You can get some ideas here http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/ :-)

Upvotes: 4

Related Questions