In this post, I will show you two methods to configure your .NET applications so that they would launch as soon as Windows starts. These two methods do not require any change to the registry, hence you don't need to worry about cleaning up that database if the user uninstalls your application.

 

First method:

From the Windows Installer Deployment Project, follow these steps:

In the File System of the project, add a special folder called User's Startup Folder.

Then a shortcut of the project output is created in the Application Folder and renamed to give it a more meaningful name.

We cut (CRTL + X) the shortcut we just created and paste it (CRTL + V) in the User's Startup Folder.

 

Second method:

The other solution is to trigger an automatic start from within the code of the application. To do this, I created an activation function, and another one for deactivation.

N.B.: For this code to work, you will need to add a reference to the Windows Script Host Object Model assembly in your project.

The activation function can be written as follow:

public void EnableApplicationStartup()
{
    string shortcutPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "MyShortcut.lnk");
    if (System.IO.File.Exists(shortcutPath)) return;
    WshShell wshShell = new WshShellClass();
    IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(shortcutPath);
    shortcut.TargetPath = Assembly.GetEntryAssembly().Location;
    shortcut.Description = "My first shortcut";
    shortcut.Save();
}


Here is the deactivation function:

public void DisableApplicationStartup()
{
    string shortcutPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "MyShortcut.lnk");
    if (!System.IO.File.Exists(shortcutPath)) return;
    System.IO.File.Delete(shortcutPath);
}

 

[Translated from a contribution by Holty Sow]