我想编辑名为“usbstor”值的注册表项,这是我在更新方法中的代码
try
{
string path = baseRegistryKey + "\\" + SubKey;
Registry.SetValue(path, KeyName, KeyValue, RegistryValueKind.DWord);
return true;
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
return false;
}
and path="HKEY_LOCAL_MACHINE\system\currentControlset\services\usbstor"keyname="start"当我运行代码时,我会得到“访问注册表项‘HKEY_LOCAL_MACHINE\system\currentControlset\services\usbstor’被拒绝“ 什么是探针?
最佳答案
可执行
HKEY_LOCAL_MACHINE
始终是注册表中 protected 空间,因此您需要将权限提升到至少 Power User
的权限或运行您的可执行文件 As Administrator
(根据您的解决方案构建的那个,应该在 ./bin
文件夹中)或禁用 UAC
。无论哪种方式,只要您没有配置/设置任何一种方式,它在 Visual Studio 中都会很麻烦。
请注意,如果您尝试使用 Run.. -> regedit
,UAC 也会提示您,因此这不仅是对您的应用程序的限制,也是对注册表本身访问的限制。
深入 Visual Studio
在打开 以管理员身份运行
之前提升 Visual Studio 足以从代码编辑注册表。
应用 list
为了将来使用,您可能需要创建 app.manifest
并将您的应用程序设置为始终需要管理员权限。
在 Solution Explorer
中右键单击您的项目,然后:Add -> New Item... -> Application Manifest File
。
在您新创建的应用程序 list 中,更改行:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
到线
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
从现在开始,如果不是以管理员身份运行,它将始终提示 UAC。
如果您以非管理员身份运行 Visual Studio
,它将尝试以管理员身份重新启动 IDE,并在继续之前提示这样做。
关于c# - 访问注册表项被拒绝当我想更新值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34639084/