10

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:

  • Is this the right tool to use for folder shortcuts?
  • How do I create a shortcut using this
  • How do I specify where the shortcut is placed

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.

Styxxy
  • 7,462
  • 3
  • 40
  • 45
swabs
  • 515
  • 1
  • 5
  • 19
  • Possible duplicate: http://stackoverflow.com/questions/3391923/placing-a-shortcut-in-users-startup-folder-to-start-with-windows –  Oct 22 '12 at 20:33
  • To get rid of your ambiguity errors, make sure when you want to use the .NET File class, you call System.IO.File, and use IWShRuntimeLibrary.File when using the other. This problem happens to me all the time if I want to use a Timer in a class that both uses System.Windows.Forms and System.Threading, because they both contain a Timer class. Sorry I cannot help with your main problem. – Aaron Deming Oct 22 '12 at 20:33
  • @ademing2, I kinda figured that's what I would have to do, but before adding that before all my System.IO.File method calls I wanted to see if I was on the right track. Thanks though – swabs Oct 22 '12 at 20:35
  • @Desolator: no, this is a duplicate of a different question, see [here](http://stackoverflow.com/questions/4897655/create-shortcut-on-desktop-c-sharp). :) – Victor Zakharov Oct 22 '12 at 20:37
  • 1
    maybe you can use an [alias](http://msdn.microsoft.com/en-us/library/sf0df423%28v=vs.110%29.aspx) to help you disambiguate System.IO.File without specifying the full namespace every time – Paolo Falabella Oct 22 '12 at 20:38
  • Not an exe shortcut, but a shortcut to a folder...unless that's the same thing, which would answer my question then. Kinda like how you can make a shortcut to a folder on a network pc is what I'm aiming for here. – swabs Oct 22 '12 at 20:41

3 Answers3

11

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

Dror
  • 1,406
  • 12
  • 15
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48
  • 1
    Exactly what I was looking for, I was getting close to this solution as well. Just wasn't naming my .lnk for the shortcut file properly – swabs Oct 22 '12 at 21:03
  • Source link is no longer valid as it has been archived. Here is the new link: [How-to-create-Windows-shortcut-(C)](http://zayko.net/post/How-to-create-Windows-shortcut-(C)) – BeaglesEnd Feb 06 '20 at 09:20
4

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();
Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • 1
    I realize it's been forever, but I'm trying to use this method to create a link to a folder named something like 2.7.1, and when I try to click on the resulting shortcut, Windows tries to open the folder like a file. Any thoughts on how to make sure Windows knows this is a shortcut to a folder? – Zach H May 21 '14 at 22:24
-1

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.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • Downvoters - I understand the link is expired now, 6 years after the answer was posted, making my answer less useful. However, this is not your typical link only answer, which deserves a downvote. – Victor Zakharov Jun 20 '18 at 12:04