首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让我的程序在启动时运行?

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

Stack Overflow用户
提问于 2009-09-04 06:26:12
回答 7查看 77.3K关注 0票数 5

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

假设我的应用程序名为windowsAplication1,并且我使用的是windows XP,并且该程序将安装在C盘上?

EN

回答 7

Stack Overflow用户

发布于 2009-09-04 06:31:48

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

代码语言:javascript
复制
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)

您可以使用以下命令将其删除

代码语言:javascript
复制
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)

上面的代码会将其添加到所有用户。您可以在以下键中将其添加到当前用户中

代码语言:javascript
复制
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

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

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

票数 29
EN

Stack Overflow用户

发布于 2013-08-04 13:39:42

简单地使用以下代码:

代码语言:javascript
复制
Dim info As New FileInfo(application.startuppath)
info.CopyTo(My.Computer.FileSystem.SpecialDirectories.Programs + "\startup\myapp.exe")

希望能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2013-09-19 21:11:52

您可以使用以下代码完成此操作

代码语言:javascript
复制
    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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1377537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档