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.
- Implement the Start-up Check Box CheckChanged event and write the following code:
- RegistryKey currentUserRegistry = Registry.CurrentUser;
- RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey
- @"SoftwareMicrosoftWindowsCurrentVersionRun", true);
- if (runRegistryKey != null)
- {
- if (StartupToolStripButton.Checked)
- runRegistryKey.SetValue("MyAppName", Application.ExecutablePath);
- else
- runRegistryKey.DeleteValue("MyAppName", false);
- }
- 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.
- 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
- We check that these registry keys already exist in the registry and check if the StartupCheckBox is checked.
- 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.
- If the checkbox is not checked we remove the key from the run and this will disable running the application at Windows Startup.
- At this point you can compile and run the application.
- Check the StartupCheckBox and close the application.
- Log off windows and log in again.
- 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.
- So we need to check the application startup option from the registry. To do so follow these steps:
- Implement the StartupForm Load event and write the following code in it this event.
- RegistryKey currentUserRegistry = Registry.CurrentUser;
- RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey
- (@"SoftwareMicrosoftWindowsCurrentVersionRun", true);
- if (runRegistryKey != null)
- {
- if (runRegistryKey.GetValue("MyAppName") == null)
- {
- StartupToolStripButton.Checked = false;
- }
- else
- {
- StartupToolStripButton.Checked = true;
- }
- }
- 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.
- We check that these keys exist.
- Then we get the startup key of our application using the runRegistryKey.GetValue passing application name key used in the first code snippet.
- 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.
- 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.
- The Application name should be the same for all method calls.
- 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.
- 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
You should read it
- What is Registry Hive?
- 50 Registry tricks to help you become a true Windows 7 / Vista 'hacker' (Part 1)
- Block access to Registry Editor on Windows 10/8/7
- How to Get Into a Computer Registry
- Use the .reg file to configure the Registry in WinXP
- How to Edit the Windows Registry
- How to re-enable Registry backup on Windows 10
- How to Make and Restore a Backup of the Windows Registry
May be interested
- How to Set Up an OpenGL SDL GLEW Template Project in Visual Studiomany programmers prefer opengl for graphics. if you are one of them, you are strongly advised by its producer, to use a window toolkit (such as sdl) and an opengl loading libraries (such as glew). this guide will help you get over the...
- How to Use Function Template Parameter Packs in C++c++ template parameter packs (also known as variadic templates) were introduced in the c++11 standard and are powerful tools that let functions accept an arbitrary number of arguments. without further ado, jump into your favourite ide,...
- How to Create a 20 Questions Game in C++this tutorial will walk you through creating 20 questions in c++ with numbers using visual studio. this tutorial is very 'bare bones' and only uses the basics of c++ programming. obtain a copy of visual studio and open it.
- How to Create a Recursive Function in C++recursion is a major topic in computer science. it is a method where a problem is reduced or simplified to smaller versions of itself until a solution is achieved. this tutorial will demonstrate how to write a simple recursive function in...
- How to Set Up OpenGL GLFW GLEW GLM on a Project with Visual Studiothis guide will help you get over the first challenge of using opengl, glfw, glew, and glm: installing and setting them up, and creating your first project with glfw-glew-glm template in visual studio 2019. it will assume your platform is...
- How to Use CMake to Get Binaries from Source Codecmake is useful in compiling source code for get binaries. compiling a library from the source code guarantees that the resulting library is perfectly tailored for your cpu/os, a luxury pre-compiled binaries don't always provide. it is...