Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / Visual Basic

Autorun Applications

Rate me:
Please Sign up or sign in to vote.
2.78/5 (11 votes)
13 Apr 2007CPOL2 min read 42.8K   542   38   2
This article will show you how to automatically run your applications at startup.

Introduction

Have you ever wondered how to get your program to run automatically when you start your computer up? Well, a simple way to do so is by adding a shortcut of your program to the "Startup" directory of the current user's Start menu, but that's not very professional (though some programs do it that way). A better way is through the Registry, which allows easier access/removal of the startup item.

Using the Code

Now, since we are accessing the Registry, you will need to import Microsoft.Win32, which contains the part of the .NET Framework that handles the Registry.

There are two different Registry directories that handle startup items: HKEY_Current_User and HKEY_Local_Machine. Current User will start the program for the current user only, and Local Machine will start the program for anyone who uses the computer.

To add a startup item to Current_User, you will have to open the Registry key and set the value, like so:

VB
Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
   Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(_
                             "Software\Microsoft\Windows\CurrentVersion\Run", True)
   key.SetValue(name, path)
End Sub

And to remove it:

VB
Private Sub RemoveCurrentKey(ByVal name As String)
   Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(_
              "Software\Microsoft\Windows\CurrentVersion\Run", True)
   key.DeleteValue(name, False)
End Sub

Now, I am using a checkbox to determine whether or not the Registry startup key is set. The "name" and "path" will be set when the function that sets the Registry value is called.

VB
If CurrentStartup.Checked Then
   AddCurrentKey("StartupExample", _
                 System.Reflection.Assembly.GetEntryAssembly.Location)
Else
   RemoveCurrentKey("StartupExample")
End If

StartupExample is the name of the project, and will also be the name set in the Registry key. System.Reflection.Assembly.GetEntryAssembly.Location gets the current location of the program to store in the Registry key. Also, another way that you can set the "name" of the startup key other than specifying it is by using System.Reflection.Assembly.GetEntryAssembly.FullName.

Now, to set the same startup key in Local_Machine, just change CurrentUser to LocalMachine.

Points of Interest

It is always a good idea to set the uninstaller for your program to remove all the Registry keys used by your program when uninstalling. This saves on Registry clutter, computer speed, and any risks of crashing when Windows tries to start a program up but can't find it because the Registry key wasn't removed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthanks for a good and easy article Pin
CinoV2-Mar-07 9:40
CinoV2-Mar-07 9:40 
GeneralRe: thanks for a good and easy article Pin
MatrixCoder3-Mar-07 5:46
MatrixCoder3-Mar-07 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.