Click here to Skip to main content
15,113,364 members
Articles / Programming Languages / Shell
Posted 17 Jan 2011


41 bookmarked

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
How to create and delete shortcut files in Windows Startup Folder using C#.


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.


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.


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:


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


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:

using IWshRuntimeLibrary;
using Shell32;

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

public void CreateStartupFolderShortcut()
  WshShellClass wshShell = new WshShellClass();
  IWshRuntimeLibrary.IWshShortcut shortcut;
  string startUpFolderPath = 

  // Create the shortcut
  shortcut = 
      startUpFolderPath + "\\" + 
      Application.ProductName + ".lnk");

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

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:

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 = 
    return link.Path;

  return String.Empty; // Not found

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

public void DeleteStartupFolderShortcuts(string targetExeName)
  string startUpFolderPath = 

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

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

    if (shortcutTargetFile.EndsWith(targetExeName, 

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


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


About the Author

Software Developer (Senior)
Denmark Denmark
No Biography provided

Comments and Discussions

Questionin Pin
muava12-Jan-15 1:44
Membermuava12-Jan-15 1:44 
QuestionStartup menu shortcuts Pin
LakDinesh23-Mar-14 21:14
professionalLakDinesh23-Mar-14 21:14 
QuestionLazy way Pin
Таньо Тодоров Иванов30-Mar-12 13:47
MemberТаньо Тодоров Иванов30-Mar-12 13:47 
AnswerRe: Lazy way Pin
PiligrimJob16-Nov-12 5:24
MemberPiligrimJob16-Nov-12 5:24 
This way sounds bad. We will have no permissions to do that in user mode under Win7 or VistaFrown | :(
BugPath Error! Pin
User 841981012-Dec-11 1:37
MemberUser 841981012-Dec-11 1:37 
GeneralRe: Path Error! Pin
Jamie Gates24-Dec-11 20:02
MemberJamie Gates24-Dec-11 20:02 
GeneralMy vote of 5 Pin
brother.gabriel3-Nov-11 12:03
Memberbrother.gabriel3-Nov-11 12:03 
GeneralMy vote of 5 Pin
Member 829629419-Oct-11 12:19
MemberMember 829629419-Oct-11 12:19 
QuestionWorks fine on 32-bit but not on 64-bit Pin
sannoblevaron29-Jun-11 21:36
Membersannoblevaron29-Jun-11 21:36 
AnswerRe: Works fine on 32-bit but not on 64-bit Pin
Ravi Bhavnani7-Mar-15 9:46
professionalRavi Bhavnani7-Mar-15 9:46 
Generalcheck this Pin
Huisheng Chen17-Jan-11 22:38
MemberHuisheng Chen17-Jan-11 22:38 
GeneralMy vote of 5 Pin
Yusuf17-Jan-11 10:22
MemberYusuf17-Jan-11 10:22 
Generalhave 5 Pin
Pranay Rana17-Jan-11 8:01
professionalPranay Rana17-Jan-11 8:01 
GeneralMy vote of 5 Pin
kdgupta8717-Jan-11 4:19
Memberkdgupta8717-Jan-11 4:19 
GeneralMy vote of 5 Pin
rht34117-Jan-11 4:15
Memberrht34117-Jan-11 4: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.