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