[英]Registry test registry key value - C# Visual Studio 2019
I've been trying to test the value
of a registry key, with no success.我一直在尝试测试注册表项的
value
,但没有成功。 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).问题:对于 value = 0 AND value = 1,此代码始终返回 false(而注册表中的实际值为
0
- 如下面的屏幕截图所示)。
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://docs.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我在 Visual Studio 2019 和 .NET 5.0 中使用 C#
Use .ToString()
method to compare those values.使用
.ToString()
方法比较这些值。
if (Registry.GetValue(rpath, rkey, value).ToString() == value.ToString())
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
You can use (int)
— Cast operator too.您也可以使用
(int)
— Cast 运算符。 (You have to change object value = 0;
if you're using this..) (您必须更改
object value = 0;
如果您正在使用它..)
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!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.