CSharp.Net Tutorial: Run on Startup

James Cloud
Applications such as anti-virus, system utilities, etc. often give the user the option to allow the program to start up automatically with Windows. If you would like to learn how to add your application to the Windows start up program, then follow the steps given in this tutorial.

Step One - Creating Functions

Warning: the source code given in this step concerns the programmatic editing of the system registry. If you edit the Window's Registry in the wrong way, by deleting certain keys or editing sensitive keys, your operating system may stop functioning properly. Always exercise caution when editing the registry.

The first step involves creating two separate functions, one for adding the application to the system start up and one for removing it from the system start up.

Create a void called EnableStartup using the following source code:

private void EnableStartup()
{
// Create the Registry Editing Object
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

// Add this Application to the System Start up
registryKey.SetValue(Application.ProductName, Application.ExecutablePath.ToString());
}

The function just created, when called, will add the application to the system start up, as described in the code's comments. Now, finally make another void named DisableStartup and use the given source code:

private void DisableStartup()
{
// Create the Registry Editing Object
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

// Disable the Application from Starting with Windows
registryKey.SetValue(Application.ProductName, false);
}

This function will disable the application from starting with Windows.

Conclusion

With the proper functions in place, all that is required to either enable or disable the application from starting with Windows is to call either the EnableStartup or the DisableStartup function. It is important to note however, that these functions should only be called with full prior knowledge and consent of the end user. Your application may be considered malware, spyware, or just plain annoying if it starts with Windows and the user doesn't want it to.

Note: I am not responsible for any malicious use or incorporation of this source code. Always give the user the option to either enable or disable this feature.

Published by James Cloud

I like to program and do basically anything that has to do with technology and computers.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.