How to make my program run at startup?

天涯浪子 提交于 2019-11-28 10:31:00
Shoban

You can add it to registry with the following code

My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)

you can remove it using

My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)

The above code will add it to all users. You can add it to current user in the following key

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Or you can add a link to your application in the "Startup" folder.

I suggest you dont do it automatically it may irritate the user. I hate when apps add them automatically to Windows Startup. Give an option for the user to run the program on windows startup.

Simpley use this code:

Dim info As New FileInfo(application.startuppath)
info.CopyTo(My.Computer.FileSystem.SpecialDirectories.Programs + "\startup\myapp.exe")

hope it helps.

Lightsteal
Imports Microsoft.Win32

...

Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", Application.ProductName, Application.ExecutablePath, RegistryValueKind.String)
Nirav

A simple method

Imports Microsoft.Win32

...

Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run",True)
regKey.SetValue(Application.ProductName, Application.ExecutablePath)
regKey.Close()

Hope it helps

You Can Do this Using the code below

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' This is where you'll need to have the program
    ' set the check box to the previous selection that
    ' the user has set. It's up to you how you do this.
    ' For now, I'll set it as "unchecked".

    CheckBox1.Checked = False

  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' The following code is a rendition of one provided by
    ' Firestarter_75, so he gets the credit here:

    Dim applicationName As String = Application.ProductName
    Dim applicationPath As String = Application.ExecutablePath

    If CheckBox1.Checked Then
      Dim regKey As Microsoft.Win32.RegistryKey
      regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
      regKey.SetValue(applicationName, """" & applicationPath & """")
      regKey.Close()
    Else
      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 If

    ' Assuming that you'll run this as a setup form of some sort
    ' and call it using .ShowDialog, simply close this form to
    ' return to the main program
    Close()
    End Sub
Mayank Mathur

Just try this code :-

FileCopy("Name.Ext", Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\Name.Ext")

Here (Name.Ext) :-

Name - Your Application's name. Ext - The Extension, it's of-course .exe

It's the simplest and best to use.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!