Alex Rivera | Logout

How do I set a program to launch at startup

Asked 2009-03-23T18:24:31.093
105

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.

Edit
Report

2 Answers

163

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);            

}
answered 2009-03-23T21:40:13.213
129

Several options, in order of preference:

  1. Add it to the current user's Startup folder. This requires the least permissions for your app to run, and gives the user the most control and feedback of what's going on. The down-side is it's a little more difficult determining whether to show the checkbox already checked next time they view that screen in your program.
  2. Add it to the 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.
  3. Create a Scheduled Task that triggers on User Login
  4. Add it to the 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.
  5. Set it up as a windows service. Only do this if you really mean it, and you know for sure you want to run this program for all users on the computer.

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.

answered 2009-03-23T18:29:08.617

Your Answer