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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With