如何让我的程序在启动时运行?

时间:2009-09-04 06:26:12

标签: vb.net startup

我正在编写一个类似于谷歌桌面的桌面应用程序但使用我自己的小工具和vb.net 2008如何在用户将其安装到计算机上以便在启动时运行时创建我的应用程序?

假设我的应用程序名称是windowsAplication1,我使用的是Windows XP,程序将安装在C盘上?

7 个答案:

答案 0 :(得分:27)

您可以使用以下代码将其添加到注册表中

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

您可以使用

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

以上代码会将其添加到所有用户。您可以使用以下密钥将其添加到当前用户

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

或者您可以在“Startup”文件夹中添加指向您的应用程序的链接。

我建议你不要自动这样做,这可能会刺激用户。我讨厌应用程序自动将它们添加到Windows Startup。为用户提供一个选项,以便在Windows启动时运行该程序。

答案 1 :(得分:1)

使用此代码简单:

Dim info As New FileInfo(application.startuppath)
info.CopyTo(My.Computer.FileSystem.SpecialDirectories.Programs + "\startup\myapp.exe")
希望它有所帮助。

答案 2 :(得分:1)

一个简单的方法

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()

希望有所帮助

答案 3 :(得分:1)

Imports Microsoft.Win32

...

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

答案 4 :(得分:0)

您可以使用以下代码执行此操作

    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

答案 5 :(得分:0)

试试这段代码: -

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

这里(Name.Ext): -

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

这是最简单和最好用的。

答案 6 :(得分:0)

可能是旧话题,但添加了 导入System.Security.AccessControl.RegistryRights 应该解决System.Security.SecurityException:'不允许请求的注册表访问。' Christhofer Natalius所说的麻烦