autostart application & toggle autostart on / off

Posted on 2004-10-12
Last Modified: 2008-01-09
what is the best method to let the user check a checkbox in my application that turns autostarting the app on windows start on and off?

Ie there should be a checkbox in the app, when it is on the app will start on windows starting, if it is off it should not start automatically.

I guess it involves setting/removing some registry keys (which ones?), or is there a simpler way?

Also it should start to Notify Icon if started automatically, but it should start "regularly" if started by double clicking the exe (no autostart) - can the app know the difference, ie if started automatically or by clicking the exe?
Question by:netslt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 34

Expert Comment

ID: 12291871
You can add a batch file to the directory C:\Documents and Settings\All Users\Start Menu\Programs\Startup\ with something like


Then add / delete it depending on what the user wants

TO tell if it was opened automatically or not, maybe you could use some cmd line args to tell

ie in tha bacth file have

c:\pathToApp\MyApp.exe -Startup


Author Comment

ID: 12292245
This could work, but do you know how to get the path to the startup folder in VB.NET?

Because it will be different depending on language / windows version.
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

LVL 34

Expert Comment

ID: 12292291
There would be an API to get that... i see what i can find
LVL 34

Expert Comment

ID: 12292334

Author Comment

ID: 12292423
Thanks, I think this should work.

Do you also know how to do it via registry because I think that would be easier because there is no bat file to be written etc.?
LVL 34

Expert Comment

ID: 12292434
>> easier

I dont like touching it... can cause lots of pain!

Author Comment

ID: 12292723
Thanks - I think I have enough information to continue.
LVL 86

Accepted Solution

Mike Tomlinson earned 250 total points
ID: 12293001
Hi netslt,

Dealing with the registry is not that painful.  =)

Here is the entire app.  The was a minor issue where the form would not hide itself properly on form load if the /tray parameter was specified, so I ended up using a timer to get around it.



Imports Microsoft.Win32

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
        Me.CheckBox1.Location = New System.Drawing.Point(8, 8)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(88, 24)
        Me.CheckBox1.TabIndex = 0
        Me.CheckBox1.Text = "CheckBox1"
        Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"), System.Drawing.Icon)
        Me.NotifyIcon1.Text = "NotifyIcon1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private AutoRunKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run\"
    Private MyAppName As String = "MyAppName"
    Private MyAppFullPath As String = Application.ExecutablePath
    Private WithEvents t As System.Timers.Timer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t = New System.Timers.Timer(50)
        t.Enabled = False

        CheckBox1.Checked = isAutoRunSet()

        Dim arguments As String() = Environment.GetCommandLineArgs()
        If arguments.GetUpperBound(0) >= 1 Then
            If arguments(1).ToLower.Equals("/tray") Then
                Me.WindowState = FormWindowState.Minimized
            End If
        End If
    End Sub

    Private Function isAutoRunSet() As Boolean
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey(AutoRunKey)
            If Not (rk Is Nothing) Then
                Dim AutoRunSet As Boolean = Not (rk.GetValue(MyAppName) Is Nothing)
                Return AutoRunSet
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Reading Registry")
        End Try
    End Function

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            t.Enabled = True
            NotifyIcon1.Visible = True
        End If
    End Sub

    Private Sub t_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles t.Elapsed
        t.Enabled = False
    End Sub

    Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
        NotifyIcon1.Visible = False
        Me.WindowState = FormWindowState.Normal
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey(AutoRunKey, True)
            If Not (rk Is Nothing) Then
                Select Case CheckBox1.Checked
                    Case True
                        rk.SetValue(MyAppName, Chr(34) & MyAppFullPath & Chr(34) & " /tray")

                    Case False

                End Select
                MsgBox(AutoRunKey, MsgBoxStyle.Information, "Error Accessing Registry")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Error Accessing Registry")
        End Try
    End Sub

End Class

Expert Comment

ID: 12294981
Can U ge the desktop startup folder path which u can see by going to start menu ->program -> start up.  If then Create a shoutcut of u'r application  in that directory When the redio button is switched on. This will start u'r application when window starts. And It will turn of delete the shourt cut it wil not start further.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question