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

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

Linda Hamilton
Linda HamiltonOriginal
2025-01-02 19:00:38572browse

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

Implementing a Startup Application in C# .NET 2.0

In this scenario, you have a checkbox option in your application that allows users to specify if they wish to have it launch automatically at startup. To achieve this functionality, let's explore the process.

To set an application to run at startup, you can utilize the Windows Registry to create a RUN key in the current user's profile. Here's how you can approach this in C# using .NET 2.0:

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:

  1. Open Registry Key: The OpenSubKey method opens the RUN subkey in the HKCU hive with write access, allowing you to manipulate its values.
  2. Create or Update Value: If the checkbox is checked (indicating that the user wants the app to run at startup), a registry value is created with the name AppName (use a unique name for your application) and the corresponding value is set to the executable path of your application.
  3. Remove Value: If the checkbox is unchecked, the registry value with the name AppName is deleted from the RUN key.

By implementing this code, you can seamlessly set your application to launch at startup based on the user's preference specified through the checkbox option.

The above is the detailed content of How to Make a C# .NET 2.0 Application Automatically Start 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