gpt4 book ai didi

c# - 无法访问注册表 HKLM key

转载 作者:行者123 更新时间:2023-11-30 14:20:17 26 4
gpt4 key购买 nike

无法从 windows xp 受限/ guest 用户帐户访问注册表 HKLM key

public int GetEnabledStatus()
{
RegistryKey hklm = Registry.LocalMachine;
int Res;
try
{
RegistryKey run1 =
hklm.OpenSubKey(@"Software\Microsoft\Windows\myApp", true);
hkcu.OpenSubKey(@"Software\Microsoft\Windows\myApp", true);
Res = int.Parse(run1.GetValue("enabled").ToString());
}
catch
{
Res = 0;
return Res;
}
finally
{
hklm.Close();
}
return Res;
}

此代码在管理员用户帐户中运行良好,在受限/ guest 帐户下调用此函数不会返回值。有什么解决办法吗

最佳答案

您正在以“写入”模式打开 key (第二个参数设置为“true”)。作为受限用户,您没有写入 HKLM 的权限。通过将模式更改为“只读”(第二个参数设置为“false”),您应该能够读取该值。

我建议按如下方式更新代码:

private static int GetEnabledStatus()
{
const int defaultStatus = 0;
using (var key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\myApp")) // open read-only
{
int valueInt;
var value = (key != null
? key.GetValue("enabled", defaultStatus)
: defaultStatus);
return (value is int
? (int)value
: int.TryParse(value.ToString(), out valueInt)
? valueInt
: defaultStatus);
}
}

不需要异常处理,当以受限用户运行时可以在我的机器上工作:-)。

关于c# - 无法访问注册表 HKLM key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1471532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com