Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / shell

Add/Remove Startup Folder Shortcut to Your App

4.85/5 (21 votes)
17 Jan 2011Public Domain2 min read 101.3K   3.3K  
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