The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries Source Code &nbsp


 NOTE: this code has been superceded by the version at the new site.



&nbsp

Setting Your Application to Automatically Run When Windows Starts

Download the RegStart Demonstration Application (27kb)

This source code shows how to automatically start an application when Windows starts using the registry. You can either have the application Auto-Start once next time Windows starts, or everytime Windows starts. The method uses my cRegistry class to make it easy to get the registry information.

To see this code working in a full sample application, check out the Goldfish clipboard cache sample application.

Here is a simple Property Get/Property Let pair which you can reuse along with cRegistry to achieve the desired auto-running method:

Public Enum eAutoRunTypes
&nbsp &nbsp eNever
&nbsp &nbsp eOnce
&nbsp &nbsp eAlways
End Enum

Public Property Let AutoRun(ByVal eType As eAutoRunTypes)
Dim sExe As String

&nbsp &nbsp sExe = App.Path
&nbsp &nbsp If (Right$(sExe, 1) "\") Then sExe = sExe & "\"
&nbsp &nbsp sExe = sExe & App.EXEName
&nbsp &nbsp
&nbsp &nbsp Dim cR As New cRegistry
&nbsp &nbsp cR.ClassKey = HKEY_LOCAL_MACHINE
&nbsp &nbsp If (eType = eNever) Then
&nbsp &nbsp &nbsp &nbsp ' Remove entry from always Run if it is there:
&nbsp &nbsp &nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
&nbsp &nbsp &nbsp &nbsp cR.ValueKey = App.EXEName
&nbsp &nbsp &nbsp &nbsp On Error Resume Next
&nbsp &nbsp &nbsp &nbsp cR.DeleteValue
&nbsp &nbsp &nbsp &nbsp Err.Clear
&nbsp &nbsp &nbsp &nbsp ' Remove entry from RunOnce if it is there:
&nbsp &nbsp &nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
&nbsp &nbsp &nbsp &nbsp On Error Resume Next
&nbsp &nbsp &nbsp &nbsp cR.DeleteValue
&nbsp &nbsp &nbsp &nbsp Err.Clear
&nbsp &nbsp ElseIf eType = eOnce Then
&nbsp &nbsp &nbsp &nbsp ' Remove entry from always Run if it is there:
&nbsp &nbsp &nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
&nbsp &nbsp &nbsp &nbsp cR.ValueKey = App.EXEName
&nbsp &nbsp &nbsp &nbsp On Error Resume Next
&nbsp &nbsp &nbsp &nbsp cR.DeleteValue
&nbsp &nbsp &nbsp &nbsp Err.Clear
&nbsp &nbsp &nbsp &nbsp ' Add an entry to RunOnce (or just ensure the exe name and path
&nbsp &nbsp &nbsp &nbsp ' is correct if it is already there):
&nbsp &nbsp &nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
&nbsp &nbsp &nbsp &nbsp cR.ValueKey = App.EXEName
&nbsp &nbsp &nbsp &nbsp cR.ValueType = REG_SZ
&nbsp &nbsp &nbsp &nbsp cR.Value = sExe
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp ' Remove entry from RunOnce if it is there:
&nbsp &nbsp &nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
&nbsp &nbsp &nbsp &nbsp cR.ValueKey = App.EXEName
&nbsp &nbsp &nbsp &nbsp On Error Resume Next
&nbsp &nbsp &nbsp &nbsp cR.DeleteValue
&nbsp &nbsp &nbsp &nbsp Err.Clear
&nbsp &nbsp &nbsp &nbsp ' Add an entry to RunOnce (or just ensure the exe name and path
&nbsp &nbsp &nbsp &nbsp ' is correct if it is already there):
&nbsp &nbsp &nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
&nbsp &nbsp &nbsp &nbsp cR.ValueKey = App.EXEName
&nbsp &nbsp &nbsp &nbsp cR.ValueType = REG_SZ
&nbsp &nbsp &nbsp &nbsp cR.Value = sExe
&nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp
End Property
Public Property Get AutoRun() As eAutoRunTypes
&nbsp &nbsp Dim cR As New cRegistry
&nbsp &nbsp cR.ClassKey = HKEY_LOCAL_MACHINE
&nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\Run"
&nbsp &nbsp cR.ValueKey = App.EXEName
&nbsp &nbsp cR.Default = "?"
&nbsp &nbsp cR.ValueType = REG_SZ
&nbsp &nbsp If (cR.Value = "?") Then
&nbsp &nbsp &nbsp &nbsp cR.SectionKey = "Software\Microsoft\Windows\CurrentVersion\RunOnce"
&nbsp &nbsp &nbsp &nbsp If (cR.Value = "?") Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp AutoRun = eNever
&nbsp &nbsp &nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp AutoRun = eOnce
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp AutoRun = eAlways
&nbsp &nbsp End If
End Property



Back to top

Back to Source Code Overview

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 23 June 1998