当我想更新值时拒绝访问注册表项 [英] access to the registry key is denied When i want update the value
问题描述
我想编辑名为usbstor"值的注册表项,这是我在更新方法中的代码
i want edit Registry key called "usbstor" value and this my code in update method
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'被拒绝"什么是problem?
and path="HKEY_LOCAL_MACHINE\system\currentControlset\services\usbstor" keyname="start" When i run the code i'll get "Access to the registry key 'HKEY_LOCAL_MACHINE\system\currentControlset\services\usbstor' is denied" what is probelm?
推荐答案
Executable
HKEY_LOCAL_MACHINE
在注册表中始终是受保护的空间,因此您需要将权限提升到至少 Power User
的权限,或者运行您的可执行文件 As Administrator
(根据您的解决方案构建的,应该在 ./bin
文件夹中)或禁用 UAC
.无论哪种方式,只要您没有配置/设置任何一种方式,在 Visual Studio 中都会很麻烦.
Executable
HKEY_LOCAL_MACHINE
is always protected space in registry, so you need to either elavate privilliges to those of at least Power User
or run your executable As Administrator
(the one built from your solution, should be in ./bin
folder) or disable UAC
. Either way it will be troublesome inside Visual Studio as long as you don't have either way configured/set.
请注意,如果您尝试使用 Run.. ->regedit
UAC 还会提示您,因此这不仅限制了您的应用程序,而且限制了访问注册表本身.
Note that if you try to use Run.. -> regedit
you are also prompted by UAC, so that's not only restriction for your app but for access to registry per se.
在打开到 Run as administrator
之前提升 Visual Studio 足以从代码编辑注册表.
Elevating Visual Studio before opening to Run as administrator
is sufficent to edit registry from code.
为了将来使用,您可能希望创建 app.manifest
并将您的应用程序设置为始终需要管理员权限.在 Solution Explorer
中右键单击您的项目,然后:Add ->新项目... ->应用程序清单文件
.在新创建的应用程序清单中,更改行:
For future usage you might want to create app.manifest
and set your application to always require administrator privileges.
Right click on your project in Solution Explorer
, then: Add -> New Item... -> Application Manifest File
.
Inside your newly created application manifest, change the line:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
到线
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
从现在开始,如果不是以管理员身份运行,它将始终提示 UAC.如果您以非管理员身份运行 Visual Studio
,它将尝试以管理员身份重新启动 IDE,并在继续之前提示您这样做.
From now on it will always prompt UAC if not run as administrator.
If you run Visual Studio
as not administrator, it will attempt to restart IDE as administrator, prompting to do so before proceeding.
这篇关于当我想更新值时拒绝访问注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!