Launch your application automatically when the system starts with C#

Launch your application automatically when the system starts with C#

In this article I will describe automatic startup of your c# application on windows machine using the registry settings. This article is somehow opposite to the one posted earlier about automatic shutdown.
To set the registry for your application to start up automatically you need to find the key responsible for automatic startup and set value of your application on this key:

             RegistryKey rkey = Registry.CurrentUser.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
             rkey.SetValue("YourApplicationName", Assembly.GetExecutingAssembly().Location);

Executing this from your program will automatically add your assembly to auto start, this is thanks to Assembly.GetExecutingAssembly().Location piece.
When running your application you can easily check if you app has the key set:

              RegistryKey rkey = Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
              if(rkey == null)
                      //not set
              if((string)rkey.GetValue("YourApplicationName") == null)
                      //not set either

If you decide that you don’t want the application to be started automatically when the system starts, you have to delete the value for the key:

              RegistryKey rkey = Registry.CurrentUser.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
              rkey.DeleteValue("YourApplicationName");

And thats it. Registry looks scary at the beginning but hey, this was fun and easy!

Leave a Reply