Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch Window on Windows startup

Tags:

c#

wpf

startup

I want that my application (a WPF Window) is launched on Windows startup. I tried different solutions, but no one seems to work. What i have to write in my code to do this?

like image 963
Nick Avatar asked Jun 16 '12 16:06

Nick


People also ask

How do I get Windows to open on startup?

If there isn't an option for Open file location, it means the app can't run at startup. With the file location open, press the Windows logo key + R, type shell:startup, then select OK. This opens the Startup folder. Copy and paste the shortcut to the app from the file location to the Startup folder.

How do I change which programs open on startup?

Type and search [Startup Apps] in the Windows search bar①, and then click [Open]②. In Startup Apps, you can sort apps by Name, Status, or Startup impact③. Find the app that you want to change, and select Enable or Disable④, the startup apps will be changed after the computer boots next time.

How do I change which programs open on startup Windows 10?

Step 1: Select the Start button, then select Settings > Apps > Startup. Step 2: Under the Startup Apps, make sure any app you want to run at startup is turned On or turn Off any app you don't want to startup. Tip: If your program isn't listed, you can change it by adding a shortcut to the Windows Startup folder.


1 Answers

You are correct when you say that you must add a key to the registry.

Add a key to:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

if you want to start the application for the current user.

Or:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 

If you want to start it for all users.

For example, starting the application for the current user:

var path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true);
key.SetValue("MyApplication", Application.ExecutablePath.ToString());

Just replace the line second line with

RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true);

if you want to automatically start the application for all users on Windows startup.

Just remove the registry value if you no longer want to start the application automatically.

As such:

var path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true);
key.DeleteValue("MyApplication", false);

This sample code was tested for a WinForms app. If you need to determine the path to the executable for a WPF app, then give the following a try.

string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

Just replace "Application.ExecutablePath.ToString()" with the path to your executable.

like image 125
Christophe Geers Avatar answered Oct 09 '22 14:10

Christophe Geers