swabs
swabs

Reputation: 515

Creating a shortcut to a folder in c#

To make a long story short, I need to to create a shortcut to a folder using C#. I've been reading on using IWshRuntimeLibrary. When I go to try anything using IWshRuntimeLibrary I get all sorts of ambiguity errors with System.IO.File. I'm assuming this is because there is a IWshRuntimeLibrary.File interface along with System.IO.File. All I've really been able to find are articles on making shortcuts to apps, not folders.

Ambiguity Errors aside:

Also when I try to create a shortcut to a folder, say C:\TEMP using this:

IWshShortcut shortcut;
wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(@"C:\TEMP");

shortcut.TargetPath = @"C:\Documents and Settings";

I get a COMException. According to what I've read this should create a shortcut to the C drive temp folder and put the shortcut in the Documents and Settings.

Upvotes: 10

Views: 17658

Answers (3)

Nesim Razon
Nesim Razon

Reputation: 9794

Dont forget to set Embed Interop Types to False to reference Interop.IWshRuntimeLibrary. I tested and works without getting an error.

  // Make sure you use try/catch block because your App may has no permissions on the target path!
  try
  {
    CreateShortcut(@"C:\temp", @"C:\MyShortcutFile.lnk",
        "Custom Shortcut", "/param", "Ctrl+F", @"c:\");
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
  }



   /// <summary>
    /// Create Windows Shorcut
    /// </summary>
    /// <param name="SourceFile">A file you want to make shortcut to</param>
    /// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
    public static void CreateShortcut(string SourceFile, string ShortcutFile)
    {
      CreateShortcut(SourceFile, ShortcutFile, null, null, null, null);
    }

    /// <summary>
    /// Create Windows Shorcut
    /// </summary>
    /// <param name="SourceFile">A file you want to make shortcut to</param>
    /// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
    /// <param name="Description">Shortcut description</param>
    /// <param name="Arguments">Command line arguments</param>
    /// <param name="HotKey">Shortcut hot key as a string, for example "Ctrl+F"</param>
    /// <param name="WorkingDirectory">"Start in" shorcut parameter</param>
    public static void CreateShortcut(string TargetPath, string ShortcutFile, string Description,
       string Arguments, string HotKey, string WorkingDirectory)
    {
      // Check necessary parameters first:
      if (String.IsNullOrEmpty(TargetPath))
        throw new ArgumentNullException("TargetPath");
      if (String.IsNullOrEmpty(ShortcutFile))
        throw new ArgumentNullException("ShortcutFile");

      // Create WshShellClass instance:
      var wshShell = new WshShellClass();

      // Create shortcut object:
      IWshRuntimeLibrary.IWshShortcut shorcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(ShortcutFile);

      // Assign shortcut properties:
      shorcut.TargetPath = TargetPath;
      shorcut.Description = Description;
      if (!String.IsNullOrEmpty(Arguments))
        shorcut.Arguments = Arguments;
      if (!String.IsNullOrEmpty(HotKey))
        shorcut.Hotkey = HotKey;
      if (!String.IsNullOrEmpty(WorkingDirectory))
        shorcut.WorkingDirectory = WorkingDirectory;

      // Save the shortcut:
      shorcut.Save();
    }

source: http://zayko.net/post/How-to-create-Windows-shortcut-(C).aspx

Upvotes: 11

Victor Zakharov
Victor Zakharov

Reputation: 26424

A solution that is using IShellLink on experts-exchange:

Creating Shortcuts in .NET

Provides a managed layer for you to create File and Folder shorcuts.

It is VB.NET, but you can convert it to C# using free converters.

Upvotes: -1

Damir Arh
Damir Arh

Reputation: 17855

You got it the other way around: you're trying to make C:\Temp a shortcut to your C:\Documents and Settings folder.

The following snippet will create a shortcut to a network folder on your desktop:

var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
IWshShortcut shortcut;
var wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(Path.Combine(desktop, @"Temp.lnk"));
shortcut.TargetPath = @"\\computername\sharename";
shortcut.Save();

Upvotes: 4

Related Questions