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

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 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 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 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 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

May be interested

  • How to turn off Fast Startup on Windows 10 and Windows 8.1 / 8How to turn off Fast Startup on Windows 10 and Windows 8.1 / 8
    you want to disable the fast startup feature, refer to the steps to disable fast startup on windows 10, 8.1 and 8 in the following article of network administrator.
  • How to Change Startup Programs in Windows 7How to Change Startup Programs in Windows 7
    startup programs are saved in a special folder on the hard drive and run automatically when windows boots up. in windows 7, the default startup program settings are similar to those of previous versions of windows. to change startup...
  • How to reuse and combine Google Forms with FormRecyclerHow to reuse and combine Google Forms with FormRecycler
    formrecycler is a great gsuite application, which helps you fill out form fields, by reusing questions from previous forms, as many as you like.
  • How to Disable Fast Startup on WindowsHow to Disable Fast Startup on Windows
    fast startup windows 11 helps your computer boot faster than usual, but it can also be the reason why your windows computer doesn't shut down completely.
  • Steps to Fix startup program error not starting with Windows 10Steps to Fix startup program error not starting with Windows 10
    the startup folder is a useful utility on windows 10. it contains programs that run as soon as your pc boots up. however, you may notice that some startup programs do not launch as they should.
  • How to Control Windows 8 Startup ProgramsHow to Control Windows 8 Startup Programs
    startup items are the programs, shortcuts, folders, and drivers that are set to run automatically when a user signs into windows 8. this may or may not cause your computer's startup loading to become slow. keep in mind that startup items...
  • Instructions for using Microsoft FormsInstructions for using Microsoft Forms
    microsoft launched microsoft forms in september 2018. it is used to create surveys, polls and puzzles. this article will show you how to use microsoft forms.
  • How to disable Startup Delay in Windows 10How to disable Startup Delay in Windows 10
    if you have a powerful pc or don't have a lot of startup programs in windows 10, then you can try to reduce or even disable startup delay completely to help your pc boot faster.
  • How to Optimize Windows StartupHow to Optimize Windows Startup
    if you're using windows, you can optimize your startup performance by making sure that your startup programs are all ones that you really need to run. as you install programs, some may automatically add themselves to your startup...
  • 7 ways to take advantage of Google Forms7 ways to take advantage of Google Forms
    google forms is a fast, easy-to-use online tool that has many great uses when you need to get information from many people. besides collecting information, google forms can also process images, videos and files that you can include in the form.