C# 不允许请求的注册表访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/562350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Requested registry access is not allowed
提问by abatishchev
I'm writing a tweak utility that modifies some keys under HKEY_CLASSES_ROOT
.
我正在编写一个调整实用程序来修改HKEY_CLASSES_ROOT
.
All works fine under Windows XP and so on. But I'm getting error Requested registry access is not allowed
under Windows 7. Vista and 2008 I guess too.
在 Windows XP 等下一切正常。但是我Requested registry access is not allowed
在 Windows 7 下遇到错误。我猜也是 Vista 和 2008。
How should I modify my code to add UAC support?
我应该如何修改我的代码以添加 UAC 支持?
采纳答案by abatishchev
app.manifest
should be like this:
app.manifest
应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
回答by Ken White
You can't write to the HKCR (or HKLM) hives in Vista and newer versions of Windows unless you have administrative privileges. Therefore, you'll either need to be logged in as an Administrator before you run your utility, give it a manifest that says it requires Administrator level (which will prompt the user for Admin login info), or quit changing things in places that non-Administrators shouldn't be playing. :-)
除非您具有管理权限,否则您无法在 Vista 和更新版本的 Windows 中写入 HKCR(或 HKLM)配置单元。因此,您要么需要在运行实用程序之前以管理员身份登录,为其提供一个清单,说明它需要管理员级别(这将提示用户输入管理员登录信息),要么在非- 管理员不应该玩。:-)
回答by Brian
As a temporary fix, users can right click the utility and select "Run as administrator."
作为临时修复,用户可以右键单击该实用程序并选择“以管理员身份运行”。
回答by Davy8
If you don't need admin privs for the entire app, or only for a few infrequent changes you can do the changes in a new process and launch it using:
如果您不需要整个应用程序的管理员权限,或者只需要很少的更改,您可以在新进程中进行更改并使用以下命令启动它:
Process.StartInfo.UseShellExecute = true;
Process.StartInfo.Verb = "runas";
which will run the process as admin to do whatever you need with the registry, but return to your app with the normal priviledges. This way it doesn't prompt the user with a UAC dialog every time it launches.
它将以管理员身份运行该过程,以使用注册表执行您需要的任何操作,但会以正常权限返回到您的应用程序。这样它就不会在每次启动时向用户提示 UAC 对话框。
回答by Ozzie
This issue has to do with granting the necessary authorization to the user account the application runs on. To read a similar situation and a detailed response for the correct solution, as documented by Microsoft, feel free to visit this post: http://rambletech.wordpress.com/2011/10/17/requested-registry-access-is-not-allowed/
此问题与向运行应用程序的用户帐户授予必要的授权有关。要阅读 Microsoft 记录的类似情况和正确解决方案的详细响应,请随时访问此帖子:http: //rambletech.wordpress.com/2011/10/17/requested-registry-access-is-不允许/
回答by Despertar
I was trying the verb = "runas"
, but I still was getting UnauthorizedAccessException when trying to update registry value. Turned out it was due to not opening the subkey with writeable set to true.
我正在尝试verb = "runas"
,但在尝试更新注册表值时仍然遇到 UnauthorizedAccessException。原来这是因为没有打开可写设置为 true 的子项。
Registry.OpenSubKey("KeyName", true);
Cannot write to Registry Key, getting UnauthorizedAccessException
回答by Last Resort
You Could Do The same as abatishchev but without the UAC
你可以做与 abatishchev 一样的事情,但没有 UAC
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
</requestedPrivileges>
</security>
</trustInfo>
</assembly>