创建注册表项时拒绝访问 [英] Access denied when creating registry key
问题描述
我正在尝试在以下位置创建注册表项,但出现访问被拒绝错误:
I am trying to create a registry key at following location but I am getting access denied error:
HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MyProgram
HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MyProgram
代码如下:
RegistryKey reg;
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MyCompany\MyProgram");
推荐答案
您很可能正在使用 用户帐户控制 (UAC).这意味着即使您是管理员,您的访问令牌也没有必要的权限来执行诸如在 HKEY_LOCAL_MACHINE
中创建注册表项之类的操作.
You are most likely using User Account Control (UAC). This means that even if you are an administrator your access token doesn't have the necessary privileges to do things like creating registry keys in HKEY_LOCAL_MACHINE
.
但是,通过 UAC 提示,您可以提升您的权限.
However, by going through a UAC prompt you can elevate your privileges.
Regedit 包含一个 UAC 清单,它将在它之前引发提示执行以确保它可以执行它需要执行的操作.您还可以右键单击可执行文件或快捷方式,然后选择以管理员身份运行.
Regedit includes a UAC manifest that will raise the prompt before it is executed ensuring that it can perform the actions it needs to be able to do. You can also right-click on an executable or shortcut and select Run as administrator.
所以基本上你有三个选择:
So essentially you have three options:
- 关闭 UAC
- 使用以管理员身份运行
- 在可执行文件中包含 UAC 清单
第一个解决方案不太安全,而最后一个解决方案是最优雅的(但实际上也是需要一些努力的).
The first solution is less secure and the last solution is the most elegant (but also the one that actually requires some effort).
这篇关于创建注册表项时拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!