Setting a Program to Launch at Startup
Many applications require the ability to start automatically when the operating system boots. This article addresses the specific question of how to set a program to launch at startup using C# with .NET 2.0.
Solution:
A common approach for setting a program to run at startup is to utilize the Windows registry. The Registry Key for storing startup applications is located at:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Within this registry key, you can create a new value with the following properties:
By setting this registry key, Windows will execute your application automatically at startup.
Implementation in C#:
The following code snippet demonstrates how to implement startup configuration in C# using the Registry API:
using Microsoft.Win32; private void SetStartup() { RegistryKey rk = Registry.CurrentUser.OpenSubKey ("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); if (chkStartUp.Checked) rk.SetValue(AppName, Application.ExecutablePath); else rk.DeleteValue(AppName, false); }
In this code, we create or delete the registry key based on the value of the chkStartUp checkbox. When the checkbox is checked, the registry key is created with the name of the application (AppName) and the path to the executable (Application.ExecutablePath). Unchecking the checkbox removes the registry key, preventing the application from launching at startup.
The above is the detailed content of How to Make a C# .NET 2.0 Application Launch at Windows Startup?. For more information, please follow other related articles on the PHP Chinese website!