在HKCU中添加注册表项“SOFTWARE \\ Microsoft \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\在SetValue上抛出错误“尝试执行未经授权的操作” [英] Adding Registry entry in HKCU "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce" throws error on SetValue "attempted to perform an unauthorized operation"

查看:194
本文介绍了在HKCU中添加注册表项“SOFTWARE \\ Microsoft \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\在SetValue上抛出错误“尝试执行未经授权的操作”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的WPF应用程序添加为仅在用户登录时自动运行一次。所以,我在应用程序启动时使用以下代码



I want to add my WPF Application to run automatically on User Login only Once. So, I am using the following code on Application Startup

public void SetStartUpRegistry()
{
     RegistryKey registryKeys = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows \\CurrentVersion\\RunOnce");
           string[] appKeyNames = registryKeys.GetSubKeyNames();

           var v = from x in appKeyNames
                   where appKeyNames.Contains("KeyName")
                   select x;

           if (v.Count() == 0)
           {
               RegistryKey regkey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true);
               string startPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs)
                  + @"\CompanyName\AppName.appref-ms";
               regkey.SetValue("KeyName", "\"" + startPath + "\"");
           }
       }





当我在VS中运行它时,此代码运行完美但是当我运行时从exe运行它或者我使用ClickOnce部署它会在regkey.SetValue行上抛出异常试图执行未经授权的操作。



我甚至可以创建新的子密钥此注册表项用regKey.CreateSubKey方法替换regkey.setValue。所以,我猜它不能是访问权限问题。



我对注册表文件拥有完整的权利,我甚至尝试过管理员帐户。



这段代码可能有什么问题?



我认为用户无需任何管理权限即可访问Registry.CurrentUser 。



如果我使用Run而不是RunOnce,代码运行正常,但我想使用RunOnce。



This code run perfectly when I run it in VS but when I run it from exe or I deploy it using ClickOnce it throws exception on regkey.SetValue line "attempted to perform an unauthorized operation".

I can even create new subkey in this Registry Key replacing regkey.setValue with regKey.CreateSubKey method. So, I guess it can't be Access Permission issue.

I have complete rights on registry files, I even tried it with administrator account.

What can be the Problem with this code?

I thought Registry.CurrentUser can be accessed by user without any administrative privileges.

The code runs properly if I use Run rather than RunOnce, but I want to use RunOnce.

推荐答案

您没有足够的权限。在Windows 7及更高版本中,使用管理帐户是不够的,还需要通过以管理员身份运行提升应用程序的权限。请试试。具体方法如下: http://www.sevenforums.com/tutorials/11841-run-administrator.html [ ^ ]。



如果有效,您可以决定创建并嵌入适当的应用程序清单以自动请求提升。当然,它不会绕过UAC对话框。请参阅: http://msdn.microsoft.com/en-us/library/bb756929.aspx [ ^ ]。



-SA
You have insufficient privileges. On Windows 7 and later, it's not enough to work with an administrative account, you also need to elevate privilege of the application through "Run As Administrator". Please try. This is how: http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

If it works, you may decide to create and embed an appropriate application manifest to request elevation automatically. Of course, it won't bypass the UAC dialog. Please see: http://msdn.microsoft.com/en-us/library/bb756929.aspx[^].

—SA


同意SAK,查看这些主题

C#Registry SetValue抛出UnauthorizedAccessException [ ^ ]

C#.NET:获取管理员权限? [ ^ ]

注册机构权限问题 [ ^ ]
Agree with SAK, check these threads
C# Registry SetValue throws UnauthorizedAccessException[^]
C#.NET: Acquire administrator rights?[^]
Registry permission problem[^]


这篇关于在HKCU中添加注册表项“SOFTWARE \\ Microsoft \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\在SetValue上抛出错误“尝试执行未经授权的操作”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆