How to Read Value of a Registry Key C#

How to read value of a registry key c#

You need to first add using Microsoft.Win32; to your code page.

Then you can begin to use the Registry classes:

try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net"))
{
if (key != null)
{
Object o = key.GetValue("Version");
if (o != null)
{
Version version = new Version(o as String); //"as" because it's REG_SZ...otherwise ToString() might be safe(r)
//do what you like with version
}
}
}
}
catch (Exception ex) //just for demonstration...it's always best to handle specific exceptions
{
//react appropriately
}

BEWARE: unless you have administrator access, you are unlikely to be able to do much in LOCAL_MACHINE. Sometimes even reading values can be a suspect operation without admin rights.

Read value of registry in c#

I see two problems in your code:

1)

// You're searching for HKEY_CURRENT_USER in HKEY_LOCAL_MACHINE
// Use Registry.CurrentUser instead.
RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"HKEY_CURRENT_USER\\MainKey", true);
string currentKey;
currentKey = reg.GetValue("Isregistered", true).ToString();

Find more about CurrentUser field here

2)
The other aspect is that either use @ or \\ not both in the registry path. i.e.

OpenSubKey(@"HKEY_CURRENT_USER\MainKey", true);

or

OpenSubKey("HKEY_CURRENT_USER\\MainKey", true);

Find more about verbatim string literals here

How to read registry value correctly in C#

You have specified User two times but you have to do that anyone of that, here is the safe way to read registry value

private void button1_Click(object sender, EventArgs e)
{
using (RegistryKey key = Registry.Users.OpenSubKey(".DEFAULT"))
{
if (key != null)
{
Object val = key.GetValue("mykey");
if (val != null)
{
MessageBox.Show(val.ToString());
}
else
{
MessageBox.Show("Null");
}
}

}
}

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

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

Reading a registry key in C#

see this http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C

Updated:

You can use RegistryKey class under Microsoft.Win32 namespace.

Some important functions of RegistryKey are as follows:

GetValue       //to get value of a key
SetValue //to set value to a key
DeleteValue //to delete value of a key
OpenSubKey //to read value of a subkey (read-only)
CreateSubKey //to create new or edit value to a subkey
DeleteSubKey //to delete a subkey
GetValueKind //to retrieve the datatype of registry key

C# Reading Registry Key/Value, Key is Always NULL

As Steve indicated, it may be a x86 vs x64 issue. I reproduced your code locally and when running under x86 my key was always null. Changing it to x64 allowed me to access the key.

You can change the target under Project Properties -> Build here:

Sample Image

There's some more detail on this here if you explicitly need an x86 key. Alternatively you can run %systemroot%\syswow64\regedit to add and edit x86 keys.

For reference; this works both as Admin and running as a standard user

Read registry key in C#

It's easy enough to test whether or not there is a systematic problem with string values containing underscores. You can simply create such a value in the registry editor and then read it into your C# program with GetValue(). When you do so you'll discover that the C# registry code doesn't lose underscores. So, there must be some other explanation for your problem.

My best guess is that your label component does not display the underscore. I'm not very familiar with the C# UI frameworks but that seems plausible. Try looking at the value of a under the debugger rather than in a label caption on your UI.

The other thing that comes to mind is that you have registry redirection because you have an x86 process running on x64, and your key is under a redirected key, HKLM\Software, for example. Perhaps if you look under the Wow6432Node you will see the underscore discrepancy.

As for managing the life of the key, the key is backed by an unmanged resource. Namely a Windows HKEY. The RegistryKey class implements IDisposable and so you should wrap your keys in a using statement.

Can't read Registry value with C#

Try this.
You have null because in default VS reading 32x registry.
You need set to 64x.

string subkey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinSAT";

RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
string value = localKey.OpenSubKey(subkey).GetValue("PrimaryAdapterString").ToString();


Related Topics



Leave a reply



Submit