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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1377537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:23:49  来源:igfitidea点击:

How to make my program run at startup?

vb.netstartup

提问by Shoban

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?

我正在编写一个类似于 Google 桌面的桌面应用程序,但是使用我自己的带有 vb.net 2008 的小工具,当用户将它安装在他们的计算机上时,如何让我的应用程序在启动时运行?

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

假设我的应用程序名称是 windowsAplication1 并且我使用的是 Windows XP 并且该程序将安装在 C 驱动器上?

回答by 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.

我建议你不要自动执行它可能会刺激用户。我讨厌应用程序自动将它们添加到 Windows 启动。为用户提供在 Windows 启动时运行程序的选项。

回答by Lightsteal

Imports Microsoft.Win32

...

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

回答by Benjli

Simpley use this code:

Simpley 使用此代码:

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

hope it helps.

希望能帮助到你。

回答by SmarttoolTechman

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

回答by 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

希望能帮助到你

回答by Mayank Mathur

Just try this code :-

试试这个代码:-

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

Here (Name.Ext) :-

这里 (Name.Ext) :-

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

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

It's the simplest and best to use.

它是最简单和最好使用的。