Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


autostart application & toggle autostart on / off

Posted on 2004-10-12
Medium Priority
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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…
Suggested Courses

661 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