Click here to Skip to main content
6,292,426 members and growing! (11,054 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Beginner

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

By Jim Hollenhorst

It's easy to create shortcuts using the Windows Script Host Object Model
C#.NET 1.0, Win2K, WinXP, Dev
Posted:3 Apr 2003
Views:135,062
Bookmarked:59 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
22 votes for this article.
Popularity: 6.23 Rating: 4.64 out of 5

1

2
1 vote, 4.5%
3
2 votes, 9.1%
4
19 votes, 86.4%
5

Sample Image - maximum width is 600 pixels

Introduction

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);
  link.TargetPath=TargetPathName;
  link.Save();

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:

                
  DesktopFolder=Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  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:

                
  DesktopFolder=Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  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.

Conclusion

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.

License

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


Member
Ultrapico Website: http://www.ultrapico.com

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

Occupation: Researcher
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
GeneralAdding to customer start menu directory PinmemberMember 35762714:08 19 Aug '08  
GeneralRe: Adding to customer start menu directory PinmemberJim Hollenhorst17:17 19 Aug '08  
QuestionAlternate credentials Pinmemberpattarom3:07 11 Apr '08  
GeneralHelp Plz Pinmemberdjsoul12:06 12 Mar '08  
GeneralSpecified cast is not valid Pinmemberlianaent11:12 3 Nov '06  
GeneralAll Users PinmemberGfw6:04 9 Sep '06  
GeneralHow to Read lnk files through WSH using C#.net Pinmembervipinkpr3:04 8 Sep '06  
GeneralProblems on creating shortcuts Pinmembermirceat9:05 13 Mar '06  
GeneralRe: Problems on creating shortcuts PinmemberJim Hollenhorst6:51 14 Mar '06  
GeneralRe: Problems on creating shortcuts Pinmembermirceat9:50 14 Mar '06  
GeneralRe: Problems on creating shortcuts Pinmembermirceat9:59 14 Mar '06  
GeneralRe: Problems on creating shortcuts PinmemberTapas Das0:13 17 Aug '07  
NewsMC++ PinsussAnonymous16:03 26 Aug '05  
GeneralRe: MC++ Pinmemberdborgohain10:48 3 Feb '06  
GeneralRe: MS VC++ .NET PinmemberAlan Hsieh12:43 29 Mar '06  
GeneralOS Pinmemberthiaguera2:54 1 Feb '05  
GeneralCan we use WSH to find highlighted icons on desktop? Pinmemberu999nik9:06 1 Oct '04  
GeneralCan this be done without a DLL being created? PinmemberJordan Bradford15:30 20 Sep '04  
GeneralRe: Can this be done without a DLL being created? PinmemberJim Hollenhorst18:29 20 Sep '04  
GeneralRe: Can this be done without a DLL being created? PinsussAnonymous20:30 20 Sep '04  
GeneralNice, but here is a better way... PinsussAnonymous3:31 4 Apr '03  
GeneralRe: Nice, but here is a better way... PinmemberJim Hollenhorst5:19 4 Apr '03  
GeneralRe: Nice, but here is a better way... PinmemberDynamite Jones8:16 6 Apr '03  
GeneralRe: Nice, but here is a better way... Pinmembermlavenne10:29 22 Oct '04  
GeneralRe: Nice, but here is a better way... Pinmemberstrabu4:27 14 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Apr 2003
Editor: Nishant Sivakumar
Copyright 2003 by Jim Hollenhorst
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project