Automatically start apps

  • Thread starter Thread starter Tony Vitonis
  • Start date Start date
T

Tony Vitonis

I've developed the little class below as what I hope is a generally-
useful way of adding programs to, and removing them from, the Registry
list of apps that start automatically with Windows. I'd be grateful
for any comments that y'all might be able to give me. Thanks.

----------

Usage:

Dim ASU As CAutoStartup
ASU = New CAutoStartup("[registry-key-value]")
...
IF ASU.Active Then ... ' If program already in auto-startup list
...
Try
ASU.Active = True ' Method 1 of interaction
Catch ex as Exception
MessageBox.Show(ex.ToString, "Whoops!")
End Try
...
ASU.Disable() ' Method 2 of interaction
...
ASU = Nothing

----------

Class:

Imports Microsoft.Win32

Public Class CAutoStartup

Private Const STARTUP_KEY As String = _
"Software\Microsoft\Windows\CurrentVersion\Run\"

Private m_Value As String
Private m_Executable As String

Public Sub New(ByVal Value As String)

m_Value = Value
m_Executable = Application.ExecutablePath

End Sub

Public Sub New(ByVal Value As String, ByVal Executable As String)

m_Value = Value

If Executable = vbNullString Then
m_Executable = Application.ExecutablePath
Else
m_Executable = Executable
End If

End Sub

Public Property Active() As Boolean

Get
Return StartupIsActive()
End Get

Set(ByVal Value As Boolean)

Select Case Value
Case True : Enable()
Case False : Disable()
End Select

End Set

End Property

Public Sub Enable()

Dim Key As RegistryKey

If Me.Active Then Exit Sub
Key = Registry.CurrentUser.OpenSubKey(STARTUP_KEY, True)

If Key Is Nothing Then
Throw New Exception("Registry key not available.")
Exit Sub
End If

Try
Key.SetValue(m_Value, m_Executable)
Catch
Throw New Exception("Unable to write registry value.")
End Try

Key = Nothing

End Sub

Public Sub Disable()

Dim Key As RegistryKey

If Not Me.Active Then Exit Sub
Key = Registry.CurrentUser.OpenSubKey(STARTUP_KEY, True)
If Key Is Nothing Then Exit Sub

Try : Key.DeleteValue(m_Value) : Catch : End Try
Key = Nothing

End Sub

Private Function StartupIsActive() As Boolean

Dim Key As RegistryKey
Dim Value As String

Key = Registry.CurrentUser.OpenSubKey(STARTUP_KEY)
If Key Is Nothing Then Return False

Value = Key.GetValue(m_Value, vbNullString)
Key = Nothing

Return (Value = m_Executable)

End Function

End Class
 
* Tony Vitonis said:
I've developed the little class below as what I hope is a generally-
useful way of adding programs to, and removing them from, the Registry
list of apps that start automatically with Windows. I'd be grateful
for any comments that y'all might be able to give me. Thanks.

You may want to extend the class with a Run Once feature. You will find
a VB6 implementation here: <http://vbaccelerator.com/article.asp?id=2520>.
 
Hi Tony,

There are two sets of startup registry keys - one for the current user,
which you have, and one for all users (HKLM). You may want to provide that as
well.

You might also like to give the option of starting from either of the
Programs/Startup folders - current user and All Users (and maybe Default
User).

Regards,
Fergus
 
Excellent suggestions from you and Mr. Wagner, thanks. I'll work on
the class at leisure and resubmit for your perusal when I'm "done".

(Very O.T.: Herr Wagner, have you heard the German word "Dorfmatraze"?
I heard it the other day, and while my German is scanty and distant, I
imagine it to mean "village mattress", something like "the town
bicycle in English.)
 
Hi Tony,

ROFL.:-)) Definitely OT.

I had to think about the town bicycle (then, of course - 'Out for a <ride>
on the town bicycle'), but village mattress has instantly recognisable charm!!

Regards,
Fergus

ps It's Dorfmatratze, but I reckon that was just a typo.
 
Back
Top