Pergunta

I am writing an application that needs admin rights to run in VB.NET (VS2012,framework 4) It is an app to protect the Hosts file from modification. I want the app to start automatically with windows with the command line argument "autorun".

So I have made a check box with the following code:

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

When I open regedit, the value is present but when I restart my system the program is not executed at all!

Is it because the app needs admin priviledges? How can I make it start AND correctly pass the command line argument?

Anticipating your answers!

Foi útil?

Solução

HKey_CurrentUser entries don't run when Windows starts. They run when the user logs in and the user's registry hive is loaded. If you want it to run when Windows starts, you'll need to use HKey_LocalMachine. Or even better, write this as a Windows Service.

Outras dicas

Application.ExecutablePath will get the .exe link, not the path, so it should be:

Application.StartupPath & " \autoupdate.exe"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top