Solved

autostart application & toggle autostart on / off

Posted on 2004-10-12
11
564 Views
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?
0
Comment
Question by:netslt
11 Comments
 
LVL 34

Expert Comment

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

runMyApp.bat
c:\pathToApp\MyApp.exe

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

??
0
 
LVL 34

Expert Comment

by:flavo
ID: 12291884
0
 

Author Comment

by:netslt
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.
0
 
LVL 34

Expert Comment

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

Expert Comment

by:flavo
ID: 12292334
Environment.GetFolderPath(Environment.SpecialFolder.Startup
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:netslt
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.?
0
 
LVL 34

Expert Comment

by:flavo
ID: 12292434
>> easier

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

Expert Comment

by:flavo
ID: 12292446
0
 

Author Comment

by:netslt
ID: 12292723
Thanks - I think I have enough information to continue.
0
 
LVL 85

Accepted Solution

by:
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.

Regards,

Idle_Mind

Imports Microsoft.Win32

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'CheckBox1
        '
        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"
        '
        'NotifyIcon1
        '
        Me.NotifyIcon1.Icon = CType(resources.GetObject("NotifyIcon1.Icon"), System.Drawing.Icon)
        Me.NotifyIcon1.Text = "NotifyIcon1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.CheckBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    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
        Try
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey(AutoRunKey)
            If Not (rk Is Nothing) Then
                Dim AutoRunSet As Boolean = Not (rk.GetValue(MyAppName) Is Nothing)
                rk.Close()
                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
        Me.Hide()
    End Sub

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

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Try
            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
                        rk.DeleteValue(MyAppName)

                End Select
                rk.Flush()
                rk.Close()
            Else
                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
0
 

Expert Comment

by:arun_sabat
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now