Solved

autostart application & toggle autostart on / off

Posted on 2004-10-12
11
571 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

930 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

21 Experts available now in Live!

Get 1:1 Help Now