Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / Shell

Add/Remove Startup Folder Shortcut to Your App

Rate me:
Please Sign up or sign in to vote.
4.85/5 (21 votes)
17 Jan 2011Public Domain2 min read 98.5K   3.2K   41   15
How to create and delete shortcut files in Windows Startup Folder using C#.

Introduction

This article shows how to create a shortcut file (.lnk) to your application in the Windows Startup folder (or any other folder). This is useful if you want your application to be started when a user logs in to Windows. The sample code also shows how to read the target file of a shortcut in order to determine if it points to your application. This is useful when you want to delete existing shortcuts to your application.

Using the code in this article, you will be able to add a checkbox with the label 'Start this application when Windows is started' to your application.

Starting your application on login by means of a Startup folder shortcut is more user friendly than starting the application as a service. It makes it visible to the user that the application is started on login, as opposed to the many update managers (from Adobe, Google, Apple) that are started in the background and can only be disabled by special tools like msconfig.

Background

There is no direct API in the .NET Framework for creating and editing shortcut files in Windows. You will have to use COM APIs exposed by the Windows Shell and Windows Script Host. Also, the shortcut file format is not documented by Microsoft.

The article shows how to create and read shortcut files from a C# application using COM interop.

Using the code

The sample code is a simple Windows Forms application with two buttons: Create Shortcut and Delete Shortcuts that allows you to test the functionality. The sample application is a Visual Studio 2010 solution.

Screenshot

The first thing you need to do is to add two references to your project. Select Add Reference, select the COM tab, and select these two components:

  • Microsoft Shell Controls and Automation
  • Windows Script Host Object Model

Click OK, and you will have two new references highlighted below:

References

For each of these new references, open their Properties window in Visual Studio and change the property Embed Interop Types from True to False.

Properties

If you don't do this, you will get the compiler error: Interop type 'IWshRuntimeLibrary.WshShellClass' cannot be embedded. Use the applicable interface instead., when using the .NET 4.0 framework.

In the C# source code, we add these two usings:

C#
using IWshRuntimeLibrary;
using Shell32;

The code that creates a shortcut file in the Startup Folder is shown below:

C#
public void CreateStartupFolderShortcut()
{
  WshShellClass wshShell = new WshShellClass();
  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();
}

We are using the WshShellClass class to create a shortcut. The shortcut TargetPath is taken from the global Application object.

In order to determine if an existing shortcut file is pointing to our application, we need to be able to read the TargetPath from a shortcut file. This is done using the code below:

C#
public string GetShortcutTargetFile(string shortcutFilename)
{
  string pathOnly = Path.GetDirectoryName(shortcutFilename);
  string filenameOnly = Path.GetFileName(shortcutFilename);

  Shell32.Shell shell = new Shell32.ShellClass();
  Shell32.Folder folder = shell.NameSpace(pathOnly);
  Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  if (folderItem != null)
  {
    Shell32.ShellLinkObject link = 
      (Shell32.ShellLinkObject)folderItem.GetLink;
    return link.Path;
  }

  return String.Empty; // Not found
}

The code that searches for, and deletes, existing shortcuts to our application is shown below:

C#
public void DeleteStartupFolderShortcuts(string targetExeName)
{
  string startUpFolderPath = 
    Environment.GetFolderPath(Environment.SpecialFolder.Startup);

  DirectoryInfo di = new DirectoryInfo(startUpFolderPath);
  FileInfo[] files = di.GetFiles("*.lnk");

  foreach (FileInfo fi in files)
  {
    string shortcutTargetFile = GetShortcutTargetFile(fi.FullName);

    if (shortcutTargetFile.EndsWith(targetExeName, 
          StringComparison.InvariantCultureIgnoreCase))
    {
      System.IO.File.Delete(fi.FullName);
    }
  }
}

Using these methods in your application, you can now implement the code behind a checkbox with the label: Start application when Windows is started.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior)
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionin vb.net? Pin
muava12-Jan-15 0:44
muava12-Jan-15 0:44 
QuestionStartup menu shortcuts Pin
LakDinesh23-Mar-14 20:14
professionalLakDinesh23-Mar-14 20:14 
QuestionLazy way Pin
Таньо Тодоров Иванов30-Mar-12 12:47
Таньо Тодоров Иванов30-Mar-12 12:47 
AnswerRe: Lazy way Pin
PiligrimJob16-Nov-12 4:24
PiligrimJob16-Nov-12 4:24 
BugPath Error! Pin
User 841981012-Dec-11 0:37
User 841981012-Dec-11 0:37 
GeneralRe: Path Error! Pin
Jamie Gates24-Dec-11 19:02
Jamie Gates24-Dec-11 19:02 
GeneralMy vote of 5 Pin
brother.gabriel3-Nov-11 11:03
brother.gabriel3-Nov-11 11:03 
GeneralMy vote of 5 Pin
Member 829629419-Oct-11 11:19
Member 829629419-Oct-11 11:19 
QuestionWorks fine on 32-bit but not on 64-bit Pin
sannoblevaron29-Jun-11 20:36
sannoblevaron29-Jun-11 20:36 
AnswerRe: Works fine on 32-bit but not on 64-bit Pin
Ravi Bhavnani7-Mar-15 8:46
professionalRavi Bhavnani7-Mar-15 8:46 
Generalcheck this Pin
Huisheng Chen17-Jan-11 21:38
Huisheng Chen17-Jan-11 21:38 
GeneralMy vote of 5 Pin
Yusuf17-Jan-11 9:22
Yusuf17-Jan-11 9:22 
Generalhave 5 Pin
Pranay Rana17-Jan-11 7:01
professionalPranay Rana17-Jan-11 7:01 
GeneralMy vote of 5 Pin
kdgupta8717-Jan-11 3:19
kdgupta8717-Jan-11 3:19 
GeneralMy vote of 5 Pin
rht34117-Jan-11 3:15
rht34117-Jan-11 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.