How to Make Your Windows Forms Application Run at Windows Startup Using C
Create a new windows application using Visual Studio 2005/2008. , Rename Form1 to Start-up form. , Add a checkbox to the StartupForm and rename it to Start-up Check Box. , Import the Microsoft.Win32 namespace using the following statement...
Step-by-Step Guide
-
Step 1: Create a new windows application using Visual Studio 2005/2008.
We create a new instance of the RegistryKey class named run Registry Key that will point to the following registry key path HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run 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. -
Step 2: Rename Form1 to Start-up form.
To do so follow these steps: , RegistryKey currentUserRegistry = Registry.CurrentUser; RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey (@"Software\Microsoft\Windows\CurrentVersion\Run"
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. , You will notice that if the application has a key in the run registry key the Startup Checkbox is checked and vice versa. -
Step 3: Add a checkbox to the StartupForm and rename it to Start-up Check Box.
-
Step 4: Import the Microsoft.Win32 namespace using the following statement:
-
Step 5: Implement the Start-up Check Box CheckChanged event and write the following code: RegistryKey currentUserRegistry = Registry.CurrentUser; RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey @"Software\Microsoft\Windows\CurrentVersion\Run"
-
Step 6: true); if (runRegistryKey != null) { if (StartupToolStripButton.Checked) runRegistryKey.SetValue("MyAppName"
-
Step 7: Application.ExecutablePath); else runRegistryKey.DeleteValue("MyAppName"
-
Step 8: 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.
-
Step 9: So we need to check the application startup option from the registry.
-
Step 10: Implement the StartupForm Load event and write the following code in it this event.
-
Step 11: Now compile the application and run the application.
Detailed Guide
We create a new instance of the RegistryKey class named run Registry Key that will point to the following registry key path HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run 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.
To do so follow these steps: , RegistryKey currentUserRegistry = Registry.CurrentUser; RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey (@"Software\Microsoft\Windows\CurrentVersion\Run"
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. , You will notice that if the application has a key in the run registry key the Startup Checkbox is checked and vice versa.
About the Author
Brian Shaw
Dedicated to helping readers learn new skills in organization and beyond.
Rate This Guide
How helpful was this guide? Click to rate: