How to Make Your Windows Forms Application Run at Windows Startup Using C

Sometimes you need to run your windows forms application when the user log in to windows. Many applications that use this behavior are Windows Live Messenger and Yahoo Messenger. In this article you will learn how to make your application...
Method 1 of 1:

Using Microsoft.Win32

This namespace contains all the classes used to manage the Registry including adding keys and removing registry keys. The first class that we use is the Registry Class. This class contains the main registry keys like Current User.

  1. How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 1How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 1
    Implement the Start-up Check Box CheckChanged event and write the following code:
    1. RegistryKey currentUserRegistry = Registry.CurrentUser;
    2. RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey
    3. @"SoftwareMicrosoftWindowsCurrentVersionRun", true);
    4. if (runRegistryKey != null)
    5. {
    6. if (StartupToolStripButton.Checked)
    7. runRegistryKey.SetValue("MyAppName", Application.ExecutablePath);
    8. else
    9. runRegistryKey.DeleteValue("MyAppName", false);
    10. }
      1. In the above code we simply create a new instance of the RegistryKey class named current User Registry and set this instance to look at the Current User logical section of the Registry.
      2. We create a new instance of the RegistryKey class named run Registry Key that will point to the following registry key path HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionRun
      3. We check that these registry keys already exist in the registry and check if the StartupCheckBox is checked.
      4. If it is checked we add a value to the Run key this value is a key value pain with the key is the name of the application that will be started at startup and the value is the application Executable Path on the hard drive.
      5. If the checkbox is not checked we remove the key from the run and this will disable running the application at Windows Startup.
      6. At this point you can compile and run the application.
    11. Check the StartupCheckBox and close the application.
    12. Log off windows and log in again.
    13. You will notice that the application will run when you login. But the StartupCheckBox is not checked and you does not know if the application should run at startup or not.
  2. How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 2How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 2
    So we need to check the application startup option from the registry. To do so follow these steps:
  3. How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 3How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 3
    Implement the StartupForm Load event and write the following code in it this event.
    1. RegistryKey currentUserRegistry = Registry.CurrentUser;
    2. RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey
    3. (@"SoftwareMicrosoftWindowsCurrentVersionRun", true);
    4. if (runRegistryKey != null)
    5. {
    6. if (runRegistryKey.GetValue("MyAppName") == null)
    7. {
    8. StartupToolStripButton.Checked = false;
    9. }
    10. else
    11. {
    12. StartupToolStripButton.Checked = true;
    13. }
    14. }
      1. In the above code, as in the previous code snippet we create two registry keys one that is mapped to Current User and the second that opens the Run registry key.
        1. We check that these keys exist.
        2. Then we get the startup key of our application using the runRegistryKey.GetValue passing application name key used in the first code snippet.
        3. If we find that the registry key value is null meaning that the application registry key is not found and so the application will not run at startup.
        4. If we found that the registry key value is not null meaning that the application registry key is found and so the application will run at startup.
          1. The Application name should be the same for all method calls.
  4. How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 4How to Make Your Windows Forms Application Run at Windows Startup Using C Picture 4
    Now compile the application and run the application. You will notice that if the application has a key in the run registry key the Startup Checkbox is checked and vice versa.
4.2 ★ | 6 Vote