Creating a shortcut to a folder in c#
c#, winforms
Solution
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
Problem
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.