Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?


2 Answers

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
}
like image 115
John Willemse Avatar answered Feb 18 '26 15:02

John Willemse


Have you tried this

          using Microsoft.Win32;
          RegistryKey myregistry = Registry.CurrentUser.OpenSubKey("MyKey");
          if (myregistry != null)
          {
           string Value=myregistry.GetValue("ID").ToString();
          }
like image 26
Rohit Avatar answered Feb 18 '26 15:02

Rohit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!