“试图执行未经授权的操作"使用 Registry.SetValue 时出错 [英] "Attempted to perform an unauthorized operation" error when using Registry.SetValue

查看:207
本文介绍了“试图执行未经授权的操作"使用 Registry.SetValue 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 VB.NET 中制作一个程序,该程序会将自身添加到 Windows 注册表中的启动中.但是,当我运行该程序时,我收到此错误消息:

I attempted to make a program in VB.NET that would add itself to startup in the Windows registry. However, when I run the program I receive this error message:

试图执行未经授权的操作.

Attempted to perform an unauthorized operation.

我尝试过更改权限并在网上使用了许多方法,但都证明都没有成功.我只是似乎没有权限干预注册表.

I have attempted to change permissions and used many methods online but they all proved to be unsuccessful. I just do not seem to have the permission to interfere in the registry.

这是我的代码:

My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "FILENAME", "FILEPATH")

我做错了什么?

推荐答案

正如 Joel 在评论中所说,你应该先打开子项,然后设置值.

As Joel stated in the comments, you should open the subkey first, and then set the value.

以下是我通常用来在 Windows 启动中添加/删除我的程序的方法:

Here's the methods I usually use to add/remove my programs to/from Windows startup:

Public Sub AddToStartup(Optional appCommand As String = "")
    Dim applicationName As String = Application.ProductName
    Dim applicationPath As String = Application.ExecutablePath
    Dim regKey As Microsoft.Win32.RegistryKey
    regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
    regKey.SetValue(applicationName, """" & applicationPath & """" & appCommand)
    regKey.Close()
End Sub

Public Sub RemoveFromStartup()
    Dim applicationName As String = Application.ProductName
    Dim regKey As Microsoft.Win32.RegistryKey
    regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
    regKey.DeleteValue(applicationName, False)
    regKey.Close()
End Sub

我在 AddToStartup 方法中使用了一个可选参数(即 appCommand),以防我想将命令参数传递给在 Windows 启动时运行的实例.例如 " -Hide" 在启动时运行时将程序隐藏在托盘中.

I use an Optional parameter (i.e., appCommand) in AddToStartup method in case I wanted to pass a command argument to the instance that runs at Windows startup. For example " -Hide" to hide the program in tray when running at startup.

附注:始终让用户可以选择是否愿意将您的应用程序添加到启动中,也​​可以选择反转.不要在没有用户许可的情况下强迫你的应用程序在启动时运行,否则用户会讨厌你:)

Side note: always give the user the option to willingly add your application to startup, and the option to reverse that. Do not force your application to run at startup without the user's permission, otherwise the user will hate you :)

这篇关于“试图执行未经授权的操作"使用 Registry.SetValue 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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