How to make my program run at startup?

大城市里の小女人 提交于 2019-11-27 03:38:58

问题


I'm programming a desktop application similar to Google desktop but with my own gadget with vb.net 2008 how can i make my application when the user install it on their computer to run at the time of start up?

Let assume that my application name is windowsAplication1 and I'm using windows XP and the program will be installed on C drive?


回答1:


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.




回答2:


Simpley use this code:

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

hope it helps.




回答3:


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



回答4:


Imports Microsoft.Win32

...

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



回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/1377537/how-to-make-my-program-run-at-startup

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