Click here to Skip to main content
Click here to Skip to main content
Go to top

Add/Remove Startup Folder Shortcut to Your App

, 17 Jan 2011
Rate this:
Please Sign up or sign in to vote.
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:

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 = 
    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:

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:

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

Share

About the Author

BoSkjoett
Software Developer (Senior)
Denmark Denmark
No Biography provided

Comments and Discussions

 
QuestionStartup menu shortcuts PinmemberLakDinesh23-Mar-14 20:14 
QuestionLazy way Pinmembertakerman30-Mar-12 12:47 
AnswerRe: Lazy way PinmemberPiligrimJob16-Nov-12 4:24 
BugPath Error! PinmemberFurquan Shafi12-Dec-11 0:37 
GeneralRe: Path Error! PinmemberJamie Gates24-Dec-11 19:02 
GeneralMy vote of 5 Pinmemberbrother.gabriel3-Nov-11 11:03 
GeneralMy vote of 5 PinmemberMember 829629419-Oct-11 11:19 
QuestionWorks fine on 32-bit but not on 64-bit PinmemberMember 772408029-Jun-11 20:36 
Generalcheck this PinmemberUnruled Boy17-Jan-11 21:38 
GeneralMy vote of 5 PinmemberYusuf17-Jan-11 9:22 
Generalhave 5 PinmemberPranay Rana17-Jan-11 7:01 
GeneralMy vote of 5 Pinmemberkdgupta8717-Jan-11 3:19 
GeneralMy vote of 5 Pinmemberrht34117-Jan-11 3:15 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 17 Jan 2011
Article Copyright 2011 by BoSkjoett
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid