0

我正在编写一个需要管理员权限才能在 VB.NET(VS2012,框架 4)中运行的应用程序,它是一个保护 Hosts 文件不被修改的应用程序。我希望应用程序使用带有命令行参数“autorun”的 Windows 自动启动。

所以我用以下代码制作了一个复选框:

Private Sub CheckBox_autoupdate_Click(sender As Object, e As EventArgs) Handles CheckBox_autoupdate.Click
        Dim oreg As RegistryKey = Registry.CurrentUser
        Dim okey As RegistryKey = oreg.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
        If CheckBox_autoupdate.Checked = True Then
            okey.SetValue("HostProtect", Application.ExecutablePath & " /autoupdate")
        Else
            okey.DeleteValue("HostProtect")
        End If
        My.Settings.Save()
    End Sub

当我打开 regedit 时,该值存在,但是当我重新启动系统时,程序根本没有执行!

是因为应用程序需要管理员权限吗?如何让它启动并正确传递命令行参数?

期待你的答案!

4

2 回答 2

0

Application.ExecutablePath将获得.exe链接,而不是路径,所以它应该是:

Application.StartupPath & " \autoupdate.exe"
于 2013-04-22T15:32:27.160 回答
0

Windows 启动时不会运行 HKey_CurrentUser 条目。它们在用户登录并加载用户的注册表配置单元时运行。如果您希望它在 Windows 启动时运行,则需要使用 HKey_LocalMachine。或者更好的是,将其编写为 Windows 服务。

于 2013-04-22T16:10:15.120 回答