1

I've been trying to test the value of a registry key, with no success. I tried a lot of suggestions but to no success. What am I missing?

Problem: this code always returns false for value = 0 AND value = 1 (while the actual value in registry is 0 - as seen in the screenshot below).

using Microsoft.Win32;

string rpath = @"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\";
string rkey = "EnableLUA";
object value = "0";
            
if ((Registry.GetValue(rpath, rkey, value) == value))
{
    Console.WriteLine("true");
}
else 
{
    Console.WriteLine("false"); 
}

// public static object? GetValue (string keyName, string? valueName, object? defaultValue);
// https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registry.getvalue?view=net-5.0

I'm using C# in Visual Studio 2019 and .NET 5.0

Regedit

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
fred
  • 23
  • 3
  • Does this answer your question? [How to check if a registry value exists using C#?](https://stackoverflow.com/questions/4276138/how-to-check-if-a-registry-value-exists-using-c) – Dilshan Apr 18 '21 at 10:06
  • Thank you Dil for your reply No, sorry - I recognize this thread (among many others) Seems like the test being executed is for the PRESENCE of the keyname. NOT the value of the keyname. Or whatever the terminology for registry is. Im getting confused myself :) I just want to check VALUE of a reg key. Very simple. – fred Apr 18 '21 at 11:03

1 Answers1

0

Use .ToString() method to compare those values.

if (Registry.GetValue(rpath, rkey, value).ToString() == value.ToString())
{
    Console.WriteLine("true");
}
else 
{
    Console.WriteLine("false"); 
}

You can use (int) — Cast operator too. (You have to change object value = 0; if you're using this..)

UPDATE :

try
{
    RegistryKey rKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System");
    object rValue = rKey.GetValue("EnableLUA");
    if (rValue == null)
    {
        Console.WriteLine("No such value!");
    }
    else if (rValue.ToString() == "0")
    {
        Console.WriteLine("true");
    }
    else if (rValue.ToString() == "1")
    {
        Console.WriteLine("false"); 
    }
}
catch (NullReferenceException)
{
    Console.WriteLine("No such subkey!");
}
Dilshan
  • 120
  • 3
  • 9
  • Thanks Dil! This solves my question! string rpath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"; string rkey = "EnableLUA"; object value = "0"; if (Registry.GetValue(rpath, rkey, value).ToString() == value.ToString() ) { etc etc ..} Only to discover that it gives a false positive for non-existing key (i.e string key = "NotExist";) - but that is another question :) Thanks again – fred Apr 18 '21 at 15:10
  • @fred defaultValue object cause that error - you can use `null` keyword there. -- I will update the answer with a better fail safe method. – Dilshan Apr 19 '21 at 14:29
  • Ok! Great thanks! Looking forward to see your suggestion. Thanks. – fred Apr 19 '21 at 19:05