Registry 测试注册表键值 - C# Visual Studio 2019
Registry test registry key value - C# Visual Studio 2019
我一直在尝试测试注册表项的 value
,但没有成功。我尝试了很多建议但没有成功。我错过了什么?
问题:此代码总是 returns false for value = 0 AND value = 1(而实际值在注册表是 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
我在 Visual Studio 2019 和 .NET 5.0
中使用 C#
Regedit
使用 .ToString()
方法比较这些值。
if (Registry.GetValue(rpath, rkey, value).ToString() == value.ToString())
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
您也可以使用 (int)
— Cast 运算符。 (如果你使用这个,你必须改变 object value = 0;
..)
更新:
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!");
}
我一直在尝试测试注册表项的 value
,但没有成功。我尝试了很多建议但没有成功。我错过了什么?
问题:此代码总是 returns false for value = 0 AND value = 1(而实际值在注册表是 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
我在 Visual Studio 2019 和 .NET 5.0
中使用 C#Regedit
使用 .ToString()
方法比较这些值。
if (Registry.GetValue(rpath, rkey, value).ToString() == value.ToString())
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
您也可以使用 (int)
— Cast 运算符。 (如果你使用这个,你必须改变 object value = 0;
..)
更新:
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!");
}