Reputation: 3033
I have a small application with a CheckBox
option that the user can set if they want the app to start with Windows.
My question is how do I actually set the app to run at startup.
ps: I'm using C# with .NET 2.0.
Upvotes: 104
Views: 158425
Reputation: 416149
Several options, in order of preference:
HKey_Current_User\Software\Microsoft\Windows\CurrentVersion\Run
registry key. The issue here is it only applies to the current user. Setting it for other users individually can be done, including the default/template user, but only by loading those registry hives one by one (including the template at C:\Users\Default\ntuser.dat
), and I'm not sure that's even possible using normal programming techniques.HKey_Local_Machine\Software\Microsoft\Windows\CurrentVersion\Run
registry key. The only problem here is it requires write access to a part of the registry which isn't often available to standard-privileged users.This answer is older now. Since I wrote this, Windows 10 was released, which changes how the Start Menu folders work... including the Startup
folder. It's not yet clear to me how easy it is to just add or remove a file in that folder without also referencing the internal database Windows uses for these locations.
Upvotes: 129
Reputation: 3033
Thanks to everyone for responding so fast. Joel, I used your option 2 and added a registry key to the "Run" folder of the current user. Here's the code I used for anyone else who's interested.
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);
}
Upvotes: 161
Reputation: 1
Important addition to other's answers using Application.ExecutablePath
: if one of the folders containing your exe contains some strange chars (like mine had C# name) then Application.ExecutablePath
will return path with different slashes which will result in it not working.
Application.ExecutablePath.Replace('/', '\\')
is more appropriate
Upvotes: 0
Reputation: 1649
Add an app to run automatically at startup in Windows 10
Step 1: Select the Windows Start button and scroll to find the app you want to run at startup.
Step 2: Right-click the app, select More, and then select Open file location. This opens the location where the shortcut to the app is saved. If there isn't an option for Open file location, it means the app can't run at startup.
Step 3: With the file location open, press the Windows logo key + R, type shell:startup, then select OK. This opens the Startup folder.
Step 4: Copy and paste or Create the shortcut to the app from the file location to the Startup folder.
Upvotes: -2
Reputation: 41
if you using wpf, the "ExecutablePath" don't work. So i made a code that work on wpf too.
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue(
"AutoStart.exe",
"\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\""
);
}
Upvotes: 4
Reputation: 1526
I found adding a shortcut to the startup folder to be the easiest way for me. I had to add a reference to "Windows Script Host Object Model" and "Microsoft.CSharp" and then used this code:
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
string shortcutAddress = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\MyAppName.lnk";
System.Reflection.Assembly curAssembly = System.Reflection.Assembly.GetExecutingAssembly();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "My App Name";
shortcut.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
shortcut.TargetPath = curAssembly.Location;
shortcut.IconLocation = AppDomain.CurrentDomain.BaseDirectory + @"MyIconName.ico";
shortcut.Save();
Upvotes: 2
Reputation: 1174
It`s a so easy solution:
To Add
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.SetValue("Your Application Name", Application.ExecutablePath);
To Remove
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.DeleteValue("Your Application Name", false);
Upvotes: 17
Reputation: 81
In addition to Xepher Dotcom's answer, folder path to Windows Startup should be coded that way:
var Startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
Upvotes: 8
Reputation: 231
Here is all way to add your program to startup for Windows Vista, 7, 8, 10
- File path
C:\Users\Bureau Briffault\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
(Visible from task manager, Running on current user login success, No admin privileges required)
C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
(Visible from task manager, Running on all user login success, Admin privileges required)
- Registry path
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
(Visible from task manager, Running on current user login success, No admin privileges required)
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
(Not visible from task manager, Running on current user login success, Running for one login time, No admin privileges required)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
(Visible from task manager, Running on all user login success, Admin privileges required)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
(Not visible from task manager, Running on all user login success, Running for one login time, Admin privileges required)
- Task scheduler
Microsoft.Win32.Taskscheduler.dll
(Not visible from task manager, Running on windows boot, Running as admin, Admin privileges required)
Upvotes: 23
Reputation: 71
/// <summary>
/// Add application to Startup of windows
/// </summary>
/// <param name="appName"></param>
/// <param name="path"></param>
public static void AddStartup(string appName, string path)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue(appName, "\"" + path + "\"");
}
}
/// <summary>
/// Remove application from Startup of windows
/// </summary>
/// <param name="appName"></param>
public static void RemoveStartup(string appName)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.DeleteValue(appName, false);
}
}
Upvotes: 7
Reputation: 11
You can do this with the win32 class in the Microsoft namespace
using Microsoft.Win32;
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("aldwin", "\"" + Application.ExecutablePath + "\"");
}
Upvotes: 0
Reputation: 16581
If an application is designed to start when Windows starts (as opposed to when a user logs in), your only option is to involve a Windows Service. Either write the application as a service, or write a simple service that exists only to launch the application.
Writing services can be tricky, and can impose restrictions that may be unacceptable for your particular case. One common design pattern is a front-end/back-end pair, with a service that does the work and an application front-end that communicates with the service to display information to the user.
On the other hand, if you just want your application to start on user login, you can use methods 1 or 2 that Joel Coehoorn listed.
Upvotes: 3
Reputation: 49319
You can create a registry entry in "HKCU\Software\Microsoft\Windows\CurrentVersion\Run", just be aware that it may work differently on Vista. Your setting might get "virtualized" because of UAC.
Upvotes: 5