How to Set a Program to Launch at Startup

How do I set a program to launch at startup

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 only problem here is it requires write access to the registry, which isn't always available.
  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 the registry, which isn't always available.
  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.

How to run a C# application at Windows startup (update)?

Doing some research, I found that it's a much better way to create a shortcut and place it in the Startup folder. More details are presented here and the code (which works and solves the problem) is:

        WshShell wshShell = new WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut;
string startUpFolderPath =
Environment.GetFolderPath(Environment.SpecialFolder.Startup);

// Create the shortcut
shortcut =
(IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(
startUpFolderPath + "\\" +
Application.ProductName + ".lnk");

shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Description = "Launch My Application";
// shortcut.IconLocation = Application.StartupPath + @"\App.ico";
shortcut.Save();

To be able to use the above code, you need to include the IWshRuntimeLibrary namespace and add the Windows Script Host Object Model reference to the project.

Other reference is here

Start an application at system start without login

I successfully added the application by using task schelduler on startup. Login and logout will not quit the application but no symbol is shown. Please add details to my side questions and I'll mark your answer as the accepted one.

Edit: Ended up using this one. If I have to configure, I stop the application in task manager and start it again by link. After that I quit the application and restart it by task scheduler manual start.

How do i run a program on startup? c#

As a non-admin, you can only set something to startup automatically for your own user account. You would use the per-user startup folder (or one of the HKCU registry keys) for this.

FOLDERID_CommonStartup

For all users, requires admin privileges to modify. Location depends on OS version and customization, but by default is either:

%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\StartUp
%ALLUSERSPROFILE%\Start Menu\Programs\StartUp

For C#, you can retrieve the path with Environment.GetFolderPath(SpecialFolder.CommonStartup).

FOLDERID_Startup

Per-user startup. Location depends on OS version and customization, but by default is either:

%APPDATA%\Microsoft\Windows\Start Menu\Programs\StartUp
%USERPROFILE%\Start Menu\Programs\StartUp

For C#, you can retrieve the path with Environment.GetFolderPath(SpecialFolder.Startup). You'd normally want to place a shortcut to your app here, but there is no managed API for this so you'll need to pinvoke or have your installer create one for you.

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

For all users, requires admin privileges to modify. For C#, can add an entry using the static Microsoft.Win32.Registry class.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Per user. To add a new entry:

const string HKCU = "HKEY_CURRENT_USER";
const string RUN_KEY = @"SOFTWARE\\Microsoft\Windows\CurrentVersion\Run";
string exePath = System.Windows.Forms.Application.ExecutablePath;
Microsoft.Win32.Registry.SetValue(HKCU + "\\" + RUN_KEY, "AppName", exePath);

Automatically run program on Windows Server startup

You should make a Windows Service; it's the only reliable way to do this.

Otherwise, you will run into problems if, for example, the user logs off.

If you want UI, you can make a separate GUI that communicates with the service (probably using WCF).



Related Topics



Leave a reply



Submit