Application Run at Start Up

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

using 

Microsoft.Win32;

Define a RegistryKey object we're going to use for accessing the path to the Run registry key:

// The path to the key where Windows looks for startup applications

RegistryKey rkApp
=Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersi
on\\Run",true);

You can change CurrentUser to LocalMachine if you want the application to run at startup for all the users of the
operating system.

Now here's the code used to make the current application run at startup:

// Add the value in the registry so that the application runs at startup

rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());

Of course, change MyApp to the name of your application as you want it to appear in the System Configuration Utility
(msconfig.exe which can be opened by typing msconfig at Start Menu ->
Run).Application.ExecutablePath.ToString()retrieves the path to the application executable that's
currently running, so we don't have to write it ourselves, not to mention that we don't normally know where the user
installs the application.

So basically what we did here is add a value to the Run registry key, containing the name of the application and the
path to the executable. That's enough for the executable to run at startup.

Now how do we disable the application from running at startup. Obviously, we delete the registry value we just
created:

// Remove the value from the registry so that the application doesn't start

rkApp.DeleteValue("MyApp", false);

And how do we check the current state of the application to see if it's set to run at startup or not.

// Check to see the current state (running at startup or not)

if (rkApp.GetValue("MyApp") == null)

   // The value doesn't exist, the application is not set to run at startup

   chkRun.Checked = false;

else

{
   // The value exists, the application is set to run at startup

   chkRun.Checked = true;

Where chkRun is a Checkbox which gets checked if the application is set to run at startup, and unchecked if it isn't.

There are a few other ways of making your application run at startup, such as adding it to the Startup folder of the
Start menu, or adding a line to Win.ini, however using the Run registry key is the most reliable and popular method

You might also like