user1722889
user1722889

Reputation:

checking whether key value exist or not using C#

I have a key as

"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\{4CHJHSDJHSJHDJS-SDGSJAD}"

in my registry. using

Registry.GetValue("keyname", "valuename", "default value")

i can retrieve any value of it. But I need to check whether "{4CHJHSDJHSJHDJS-SDGSJAD}" exists in the registry or not. Can anybody suggest me what check I should use to do this?

Upvotes: 3

Views: 11680

Answers (3)

Parimal Raj
Parimal Raj

Reputation: 20585

You can query for the Registry key with Registry.CurrentUser and then OpenSubKey.

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\{4CHJHSDJHSJHDJS-SDGSJAD}");

if (key != null)
{
    // key exists
}
else
{
    // key does not exists
}

Upvotes: 1

Rohit
Rohit

Reputation: 10236

Have you tried this

          using Microsoft.Win32;
          RegistryKey myregistry = Registry.CurrentUser.OpenSubKey("MyKey");
          if (myregistry != null)
          {
           string Value=myregistry.GetValue("ID").ToString();
          }

Upvotes: 3

John Willemse
John Willemse

Reputation: 6698

With a registry key, you can try to get it with the OpenSubKey method. If the returned value is null, then the key does not exist. I'm talking about keys here, not values.

In your example, that would come down to:

var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\{4CHJHSDJHSJHDJS-SDGSJAD}");
if (key == null)
{
    // Key does not exist
}
else
{
    // Key exists
}

Upvotes: 7

Related Questions