Home >Backend Development >C++ >How to Make a C# .NET 2.0 Application Launch at Windows Startup?

How to Make a C# .NET 2.0 Application Launch at Windows Startup?

Barbara Streisand
Barbara StreisandOriginal
2025-01-02 16:43:40266browse

How to Make a C# .NET 2.0 Application Launch at Windows Startup?

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:

  • Name: The unique name of your application.
  • Value: The path to your application's executable file.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn