Click here to Skip to main content
13,045,019 members (39,881 online)
Click here to Skip to main content
Add your own
alternative version


85 bookmarked
Posted 3 Apr 2003

Creating Shell Links (Shortcuts) in .NET Programs Using WSH

, 3 Apr 2003
Rate this:
Please Sign up or sign in to vote.
It's easy to create shortcuts using the Windows Script Host Object Model

Sample Image - maximum width is 600 pixels

<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (


etc) -->


It should be a simple matter to programatically create a shortcut on the desktop that links to a particular file. Unfortunately, as far as I know, the .NET Framework does not support this directly. However, by using the Windows Scripting Host Object Model, it takes only a few lines of code to create a shortcut anywhere within the file system. The purpose of this article is to show how to do this and to provide a class with methods that make it even simpler. The demonstration program uses this class to test for the existence of shortcuts and create or delete them with the click of a checkbox.

Shell Links

A standard feature of the Windows operating system is the ability to create a file that provides a link to any other file within the Windows file system. These so-called "Shortcuts" or "Shell Links" are used in many ways. Examples include the shortcuts that appear as icons on the Windows Desktop, the Start menu, the Quick Launch area within the Task Bar, and the "Send To" menu that appears when right-clicking a file in Explorer. These shortcuts are easy to create manually in the Windows GUI, but within a .NET program they typically require the programmer to use Interop.

Windows Scripting Host

The Windows Scripting Host (WSH) enables a number of file system and network operations to be performed from a script file. Fortunately, it is very simple to directly program the WSH in a .NET program by including a reference to the WSH runtime library (IWshRuntimeLibrary). To do this within the Visual Studio .NET IDE, do the following: after creating a new project, right-click on the project name within the Solution Explorer, select "Add Reference", select the "COM" tab, find and select the "Windows Script Host Object Model" in the listbox, click "Select", and then click "OK". Next, include a reference to the library, for example, within a C# file use the following:

using IWshRuntimeLibrary;

Once this groundwork is done, it is nearly trivial to create a Shell Link. 

Creating a Shell Link

The following code is all that is necessary to create a Shell Link using the Windows Scripting Host.

WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(LinkPathName);

The IWshShortcut object has a number of properties that allow the programmer to set things like the target path, the icon, the file name, the arguments, etc. You can explore these within Visual Studio using the Object Browser or Intellisense.

The Link Class

The demonstration program includes a class named Link that encapsulates static methods to test for the presence of a shortcut and to create or delete one if necessary. For example, to test for the presence of a desktop shortcut named "Shell Link", use the following code:

if(Link.Exists(DesktopFolder, "Shell Link"))DoSomething();


To create or delete a desktop shortcut named "Shell Link" that points to a file whose path is given by TargetPathName, use the following code:

Link.Update(DesktopFolder, TargetPathName, "Shell Link", Create);

This will create a link if Create is true and delete it if Create is false. If no change is needed, the code will do nothing.


That's all there is to it! It is simple to incorporate WSH in a .NET program. Creating Shell Links is one good reason to do so. You may find other uses for WSH in your programs such as mapping network drives, setting up printer connections, determining the free space on a disk, or any other tasks that can be automated with the Windows Scripting Host.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jim Hollenhorst
United States United States
Ultrapico Website:

Download Expresso 3.0, the latest version of the award-winning regular expression development tool.

You may also be interested in...


Comments and Discussions

Questionreference not available in VS 2012 Pin
Husam khiry28-Oct-14 22:52
professionalHusam khiry28-Oct-14 22:52 
AnswerRe: reference not available in VS 2012 Pin
Jim Hollenhorst30-Nov-14 15:11
memberJim Hollenhorst30-Nov-14 15:11 
QuestionException coming when creating shortcut for remote PDF file Pin
Aarvey17-Jul-13 3:46
memberAarvey17-Jul-13 3:46 
AnswerRe: Exception coming when creating shortcut for remote PDF file Pin
Aarvey18-Jul-13 1:23
memberAarvey18-Jul-13 1:23 
QuestionError Creating a Link Pin
arbh8921-Nov-12 4:22
memberarbh8921-Nov-12 4:22 
GeneralMy vote of 5 Pin
Andrewpeter16-Sep-12 17:04
memberAndrewpeter16-Sep-12 17:04 
GeneralRe: My vote of 5 Pin
Jim Hollenhorst27-Sep-12 15:45
memberJim Hollenhorst27-Sep-12 15:45 
QuestionHow to create shortcut without refercene "Windows Script Host"? Pin
Andrewpeter16-Sep-12 17:04
memberAndrewpeter16-Sep-12 17:04 
AnswerRe: How to create shortcut without refercene "Windows Script Host"? Pin
Jim Hollenhorst27-Sep-12 15:49
memberJim Hollenhorst27-Sep-12 15:49 
AnswerRe: How to create shortcut without refercene "Windows Script Host"? Pin
Kerem Guemruekcue13-Nov-12 23:43
memberKerem Guemruekcue13-Nov-12 23:43 

you do have several options here: you either use the shell api from the windows shell interface, or you can use late binding in your case, and even some more, but i personally recommend late binding, since it is easy to use from any .netapplication and is pretty clean if you know how to work with com from .net. the interface itself to the shirtcut looks like this:

<pre lang="c++">
helpstring("Shortcut Object"),
interface IWshShortcut : IDispatch {
[id(00000000), propget]
HRESULT FullName([out, retval] BSTR* out_FullName);
[id(0x000003e8), propget]
HRESULT Arguments([out, retval] BSTR* out_Arguments);
[id(0x000003e8), propput]
HRESULT Arguments([in] BSTR out_Arguments);
[id(0x000003e9), propget]
HRESULT Description([out, retval] BSTR* out_Description);
[id(0x000003e9), propput]
HRESULT Description([in] BSTR out_Description);
[id(0x000003ea), propget]
HRESULT Hotkey([out, retval] BSTR* out_HotKey);
[id(0x000003ea), propput]
HRESULT Hotkey([in] BSTR out_HotKey);
[id(0x000003eb), propget]
HRESULT IconLocation([out, retval] BSTR* out_IconPath);
[id(0x000003eb), propput]
HRESULT IconLocation([in] BSTR out_IconPath);
[id(0x000003ec), propput]
HRESULT RelativePath([in] BSTR rhs);
[id(0x000003ed), propget]
HRESULT TargetPath([out, retval] BSTR* out_Path);
[id(0x000003ed), propput]
HRESULT TargetPath([in] BSTR out_Path);
[id(0x000003ee), propget]
HRESULT WindowStyle([out, retval] int* out_ShowCmd);
[id(0x000003ee), propput]
HRESULT WindowStyle([in] int out_ShowCmd);
[id(0x000003ef), propget]
HRESULT WorkingDirectory([out, retval] BSTR* out_WorkingDirectory);
[id(0x000003ef), propput]
HRESULT WorkingDirectory([in] BSTR out_WorkingDirectory);
[id(0x000007d0), hidden]
HRESULT Load([in] BSTR PathLink);

A simple call would look like this:

<pre lang="c#">
private void button1_Click_1(object sender, EventArgs e)
object WScriptShell = null;
IWshShortcut ShellShortCut = null;

Type WSHShell = Type.GetTypeFromProgID("WScript.Shell");
WScriptShell = Activator.CreateInstance(WSHShell);

ShellShortCut = (IWshShortcut)WSHShell.InvokeMember(
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
new object[] { Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), @"SelfShortcut.lnk") });

ShellShortCut.WorkingDirectory = Environment.CurrentDirectory;
ShellShortCut.TargetPath = Application.ExecutablePath;

catch (Exception err)
MessageBox.Show("Failed to create shortcut. Reason: " + err.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if (ShellShortCut != null)

if (WScriptShell != null)
Marshal.ReleaseComObject(WScriptShell); //release com object to avoid rcw memory leak

The cleanup in the finally block is important to avoid memory (rcw) leaks.

Hope this helps,...



Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
"This reply is provided as is, without warranty express or implied."
GeneralMy vote of 4 Pin
geekbond11-Aug-12 22:49
membergeekbond11-Aug-12 22:49 
GeneralMy vote of 4 Pin
Burak Tunçbilek17-Jun-12 2:24
memberBurak Tunçbilek17-Jun-12 2:24 
QuestionMyShortcut.TargetPath with a space to make a parameter for the target app Pin
Member 31040542-Feb-10 1:00
memberMember 31040542-Feb-10 1:00 
AnswerRe: MyShortcut.TargetPath with a space to make a parameter for the target app Pin
Member 31040542-Feb-10 1:11
memberMember 31040542-Feb-10 1:11 
GeneralGreat Method Pin
HiteshSharma13-Aug-09 6:47
memberHiteshSharma13-Aug-09 6:47 
GeneralRe: Great Method Pin
Jim Hollenhorst16-Aug-09 10:00
memberJim Hollenhorst16-Aug-09 10:00 
GeneralRe: Great Method Pin
HiteshSharma16-Aug-09 10:29
memberHiteshSharma16-Aug-09 10:29 
GeneralRe: Great Method Pin
jimpar10-Feb-11 5:53
memberjimpar10-Feb-11 5:53 
GeneralRe: Great Method Pin
Jim Hollenhorst13-Mar-11 15:35
memberJim Hollenhorst13-Mar-11 15:35 
GeneralShortcut but no update Pin
mrpdude5-Aug-09 17:28
membermrpdude5-Aug-09 17:28 
GeneralShortcut but no update Pin
mrpdude5-Aug-09 17:27
membermrpdude5-Aug-09 17:27 
GeneralAdding to customer start menu directory Pin
Member 35762719-Aug-08 13:08
memberMember 35762719-Aug-08 13:08 
GeneralRe: Adding to customer start menu directory Pin
Jim Hollenhorst19-Aug-08 16:17
memberJim Hollenhorst19-Aug-08 16:17 
QuestionAlternate credentials Pin
pattarom11-Apr-08 2:07
memberpattarom11-Apr-08 2:07 
GeneralHelp Plz Pin
djsoul12-Mar-08 11:06
memberdjsoul12-Mar-08 11:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 4 Apr 2003
Article Copyright 2003 by Jim Hollenhorst
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid