Skip to content
Amal Hashim
All posts
Snippet

C# Starting Application on Windows Startup

Two ways to start a C# application automatically at Windows startup: the Startup folder or a Run registry key.

1 min read#C##Windows

Sometimes an application needs to load automatically when Windows starts. There are two common ways.

1. The Startup folder

Environment.GetFolderPath(Environment.SpecialFolder.Startup) returns the path of the Startup folder. Once you have it, use File.Copy to place a shortcut to your application there.

2. The registry

This registers the application to run for the current user. To start it for all users, use Registry.LocalMachine instead of Registry.CurrentUser.

csharp
private void RegisterStartupApplication()
{
    RegistryKey regKey = Registry.CurrentUser.OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    regKey.SetValue("MyApplicationsName", Application.ExecutablePath);
}

private void UnRegisterStartupApplication()
{
    RegistryKey regKey = Registry.CurrentUser.OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    regKey.DeleteValue("MyApplicationsName");
}