autostart application & toggle autostart on / off

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?
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

netsltAuthor Commented:
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.
There would be an API to get that... i see what i can find
netsltAuthor Commented:
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.?
>> easier

I dont like touching it... can cause lots of pain!
netsltAuthor Commented:
Thanks - I think I have enough information to continue.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.