I want to create a shortcut pointing to some EXE file, on the desktop, using .NET Framework 3.5 and relying on an official Windows API. How can I do that?
Create a shortcut on Desktop
180.6k views Asked by Vipin Arora AtThere are 15 answers
Here's a (Tested) Extension Method, with comments to help you out.
using IWshRuntimeLibrary;
using System;
namespace Extensions
{
public static class XShortCut
{
/// <summary>
/// Creates a shortcut in the startup folder from a exe as found in the current directory.
/// </summary>
/// <param name="exeName">The exe name e.g. test.exe as found in the current directory</param>
/// <param name="startIn">The shortcut's "Start In" folder</param>
/// <param name="description">The shortcut's description</param>
/// <returns>The folder path where created</returns>
public static string CreateShortCutInStartUpFolder(string exeName, string startIn, string description)
{
var startupFolderPath = Environment.SpecialFolder.Startup.GetFolderPath();
var linkPath = startupFolderPath + @"\" + exeName + "-Shortcut.lnk";
var targetPath = Environment.CurrentDirectory + @"\" + exeName;
XFile.Delete(linkPath);
Create(linkPath, targetPath, startIn, description);
return startupFolderPath;
}
/// <summary>
/// Create a shortcut
/// </summary>
/// <param name="fullPathToLink">the full path to the shortcut to be created</param>
/// <param name="fullPathToTargetExe">the full path to the exe to 'really execute'</param>
/// <param name="startIn">Start in this folder</param>
/// <param name="description">Description for the link</param>
public static void Create(string fullPathToLink, string fullPathToTargetExe, string startIn, string description)
{
var shell = new WshShell();
var link = (IWshShortcut)shell.CreateShortcut(fullPathToLink);
link.IconLocation = fullPathToTargetExe;
link.TargetPath = fullPathToTargetExe;
link.Description = description;
link.WorkingDirectory = startIn;
link.Save();
}
}
}
And an example of use:
XShortCut.CreateShortCutInStartUpFolder(THEEXENAME,
Environment.CurrentDirectory,
"Starts some executable in the current directory of application");
1st parm sets the exe name (found in the current directory) 2nd parm is the "Start In" folder and 3rd parm is the shortcut description.
The naming convention of the link leaves no ambiguity as to what it will do. To test the link just double click it.
Final Note: the application itself (target) must have an ICON image associated with it. The link is easily able to locate the ICON within the exe. If the target application has more than one icon, you may open the link's properties and change the icon to any other found in the exe.
URL shortcut
private void urlShortcutToDesktop(string linkName, string linkUrl)
{
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
{
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=" + linkUrl);
}
}
Application shortcut
private void appShortcutToDesktop(string linkName)
{
string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
{
string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=file:///" + app);
writer.WriteLine("IconIndex=0");
string icon = app.Replace('\\', '/');
writer.WriteLine("IconFile=" + icon);
}
}
Also check this example.
If you want to use some API specific functions then you will want to use the IShellLink interface
as well as the IPersistFile interface
(through COM interop).
Here is an article that goes into detail what you need to do it, as well as sample code.
You can use this ShellLink.cs class to create the shortcut.
To get the desktop directory, use:
var dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
or use Environment.SpecialFolder.CommonDesktopDirectory
to create it for all users.
With additional options such as hotkey, description etc.
At first, Project > Add Reference > COM > Windows Script Host Object Model.
using IWshRuntimeLibrary;
private void CreateShortcut()
{
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for a Notepad";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
shortcut.Save();
}
Here is a piece of code that has no dependency on an external COM object (WSH), and supports 32-bit and 64-bit programs:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace TestShortcut
{
class Program
{
static void Main(string[] args)
{
IShellLink link = (IShellLink)new ShellLink();
// setup shortcut information
link.SetDescription("My Description");
link.SetPath(@"c:\MyPath\MyProgram.exe");
// save it
IPersistFile file = (IPersistFile)link;
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
file.Save(Path.Combine(desktopPath, "MyLink.lnk"), false);
}
}
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
internal class ShellLink
{
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
internal interface IShellLink
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(out short pwHotkey);
void SetHotkey(short wHotkey);
void GetShowCmd(out int piShowCmd);
void SetShowCmd(int iShowCmd);
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
void Resolve(IntPtr hwnd, int fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
}
private void CreateShortcut(string executablePath, string name)
{
CMDexec("echo Set oWS = WScript.CreateObject('WScript.Shell') > CreateShortcut.vbs");
CMDexec("echo sLinkFile = '" + Environment.GetEnvironmentVariable("homedrive") + "\\users\\" + Environment.GetEnvironmentVariable("username") + "\\desktop\\" + name + ".ink' >> CreateShortcut.vbs");
CMDexec("echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs");
CMDexec("echo oLink.TargetPath = '" + executablePath + "' >> CreateShortcut.vbs");
CMDexec("echo oLink.Save >> CreateShortcut.vbs");
CMDexec("cscript CreateShortcut.vbs");
CMDexec("del CreateShortcut.vbs");
}
For Windows Vista/7/8/10, you can create a symlink instead via mklink
.
Process.Start("cmd.exe", $"/c mklink {linkName} {applicationPath}");
Alternatively, call CreateSymbolicLink
via P/Invoke.
I have created a wrapper class based on Rustam Irzaev's answer with use of IWshRuntimeLibrary.
IWshRuntimeLibrary -> References -> COM > Windows Script Host Object Model
using System;
using System.IO;
using IWshRuntimeLibrary;
using File = System.IO.File;
public static class Shortcut
{
public static void CreateShortcut(string originalFilePathAndName, string destinationSavePath)
{
string fileName = Path.GetFileNameWithoutExtension(originalFilePathAndName);
string originalFilePath = Path.GetDirectoryName(originalFilePathAndName);
string link = destinationSavePath + Path.DirectorySeparatorChar + fileName + ".lnk";
var shell = new WshShell();
var shortcut = shell.CreateShortcut(link) as IWshShortcut;
if (shortcut != null)
{
shortcut.TargetPath = originalFilePathAndName;
shortcut.WorkingDirectory = originalFilePath;
shortcut.Save();
}
}
public static void CreateStartupShortcut()
{
CreateShortcut(System.Reflection.Assembly.GetEntryAssembly()?.Location, Environment.GetFolderPath(Environment.SpecialFolder.Startup));
}
public static void DeleteShortcut(string originalFilePathAndName, string destinationSavePath)
{
string fileName = Path.GetFileNameWithoutExtension(originalFilePathAndName);
string originalFilePath = Path.GetDirectoryName(originalFilePathAndName);
string link = destinationSavePath + Path.DirectorySeparatorChar + fileName + ".lnk";
if (File.Exists(link)) File.Delete(link);
}
public static void DeleteStartupShortcut()
{
DeleteShortcut(System.Reflection.Assembly.GetEntryAssembly()?.Location, Environment.GetFolderPath(Environment.SpecialFolder.Startup));
}
}
VB rewrite of the Windows API IShellLink interface:
<ComImport(), Guid("00021401-0000-0000-C000-000000000046")>
Private Class ShellLink
End Class
<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")>
Private Interface IShellLink
Sub GetPath(<Out, MarshalAs(UnmanagedType.LPWStr)> ByVal pszFile As StringBuilder, ByVal cchMaxPath As Integer, <Out> ByRef pfd As IntPtr, ByVal fFlags As Integer)
Sub GetIDList(<Out> ByRef ppidl As IntPtr)
Sub SetIDList(ByVal pidl As IntPtr)
Sub GetDescription(<Out, MarshalAs(UnmanagedType.LPWStr)> ByVal pszName As StringBuilder, ByVal cchMaxName As Integer)
Sub SetDescription(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszName As String)
Sub GetWorkingDirectory(<Out, MarshalAs(UnmanagedType.LPWStr)> ByVal pszDir As StringBuilder, ByVal cchMaxPath As Integer)
Sub SetWorkingDirectory(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszDir As String)
Sub GetArguments(<Out, MarshalAs(UnmanagedType.LPWStr)> ByVal pszArgs As StringBuilder, ByVal cchMaxPath As Integer)
Sub SetArguments(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszArgs As String)
Sub GetHotkey(<Out> ByRef pwHotkey As Short)
Sub SetHotkey(ByVal wHotkey As Short)
Sub GetShowCmd(<Out> ByRef piShowCmd As Integer)
Sub SetShowCmd(ByVal iShowCmd As Integer)
Sub GetIconLocation(<Out, MarshalAs(UnmanagedType.LPWStr)> ByVal pszIconPath As StringBuilder, ByVal cchIconPath As Integer, <Out> ByRef piIcon As Integer)
Sub SetIconLocation(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszIconPath As String, ByVal iIcon As Integer)
Sub SetRelativePath(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszPathRel As String, ByVal dwReserved As Integer)
Sub Resolve(ByVal hwnd As IntPtr, ByVal fFlags As Integer)
Sub SetPath(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszFile As String)
End Interface
'How to use:
Public Shared Sub CreateNewShortcut(LNKLocation As String, LNKTarget As String, Optional TargetArgs As String = Nothing, Optional StartFolder As String = Nothing,Optional Description As String = Nothing, Optional IconFile As String = "c:\windows\System32\SHELL32.dll", Optional IconIndex As Integer = 21)
Dim link As IShellLink = CType(New ShellLink(), IShellLink)
If Description <> Nothing Then link.SetDescription(Description)
If TargetArgs <> Nothing Then link.SetArguments(TargetArgs)
If IconFile <> Nothing Then link.SetIconLocation(IconFile, IconIndex)
link.SetPath(LNKTarget)
Dim file As System.Runtime.InteropServices.ComTypes.IPersistFile = CType(link, System.Runtime.InteropServices.ComTypes.IPersistFile)
file.Save(LNKLocation, False)
End Sub
I use "Windows Script Host Object Model" reference to create shortcut.
and to create shortcut on specific location:
void CreateShortcut(string linkPath, string filename)
{
// Create shortcut dir if not exists
if (!Directory.Exists(linkPath))
Directory.CreateDirectory(linkPath);
// shortcut file name
string linkName = Path.ChangeExtension(Path.GetFileName(filename), ".lnk");
// COM object instance/props
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(linkName);
sc.Description = "some desc";
//shortcut.IconLocation = @"C:\...";
sc.TargetPath = linkPath;
// save shortcut to target
sc.Save();
}
I Use simply for my app:
using IWshRuntimeLibrary; // > Ref > COM > Windows Script Host Object
...
private static void CreateShortcut()
{
string link = Environment.GetFolderPath( Environment.SpecialFolder.Desktop )
+ Path.DirectorySeparatorChar + Application.ProductName + ".lnk";
var shell = new WshShell();
var shortcut = shell.CreateShortcut( link ) as IWshShortcut;
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
//shortcut...
shortcut.Save();
}
Use ShellLink.cs at vbAccelerator to create your shortcut easily !
private static void AddShortCut()
{
using (ShellLink shortcut = new ShellLink())
{
shortcut.Target = Application.ExecutablePath;
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
shortcut.Description = "My Shorcut";
shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
shortcut.Save(SHORTCUT_FILEPATH);
}
}
Without additional reference:
using System;
using System.Runtime.InteropServices;
public class Shortcut
{
private static Type m_type = Type.GetTypeFromProgID("WScript.Shell");
private static object m_shell = Activator.CreateInstance(m_type);
[ComImport, TypeLibType((short)0x1040), Guid("F935DC23-1CF0-11D0-ADB9-00C04FD58A0B")]
private interface IWshShortcut
{
[DispId(0)]
string FullName { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0)] get; }
[DispId(0x3e8)]
string Arguments { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] set; }
[DispId(0x3e9)]
string Description { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] set; }
[DispId(0x3ea)]
string Hotkey { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] set; }
[DispId(0x3eb)]
string IconLocation { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] set; }
[DispId(0x3ec)]
string RelativePath { [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ec)] set; }
[DispId(0x3ed)]
string TargetPath { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] set; }
[DispId(0x3ee)]
int WindowStyle { [DispId(0x3ee)] get; [param: In] [DispId(0x3ee)] set; }
[DispId(0x3ef)]
string WorkingDirectory { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] set; }
[TypeLibFunc((short)0x40), DispId(0x7d0)]
void Load([In, MarshalAs(UnmanagedType.BStr)] string PathLink);
[DispId(0x7d1)]
void Save();
}
public static void Create(string fileName, string targetPath, string arguments, string workingDirectory, string description, string hotkey, string iconPath)
{
IWshShortcut shortcut = (IWshShortcut)m_type.InvokeMember("CreateShortcut", System.Reflection.BindingFlags.InvokeMethod, null, m_shell, new object[] { fileName });
shortcut.Description = description;
shortcut.Hotkey = hotkey;
shortcut.TargetPath = targetPath;
shortcut.WorkingDirectory = workingDirectory;
shortcut.Arguments = arguments;
if (!string.IsNullOrEmpty(iconPath))
shortcut.IconLocation = iconPath;
shortcut.Save();
}
}
To create Shortcut on Desktop:
string lnkFileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Notepad.lnk");
Shortcut.Create(lnkFileName,
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "notepad.exe"),
null, null, "Open Notepad", "Ctrl+Shift+N", null);
Here's my code: