Click here to Skip to main content
Click here to Skip to main content

Create conditional shortcuts during the installation of a .NET application

By , 22 Jan 2006
 

Sample Image - Shortcut.jpg

Introduction

This is a very small and simple application yet very important as it makes your installer look more professional. This article shows how to add shortcuts of your .NET application to the User's Desktop, User's Programs Menu, and the Quick Launch Bar, based on the user's choice, during installation.

.NET setup projects allow a developer to create shortcuts and place them in the required folder, but the same freedom is not available to the end user who uses your set up to install the application, i.e., the user doesn't have the choice whether to have shortcuts or not. This application gives the user the freedom by which he can select which shortcuts he should add.

There are two important things you should know. First, the file extension of shortcut files (.lnk), and the methods to access system's Special Folders. Our logic is very simple, we are just moving shortcut files from the Application folder to the respective Special Folders.

.NET provides us with classes and methods by which we can find the Special Folders (Desktop, My Documents, Start Up etc.) at runtime. System.Environment.GetFolderPath() helps us to access the path of Special Folders at runtime. It takes as parameter Environment.SpecialFolder.

Using the Demo Application

The demo application is provided with the required tool tips which will help you to use the application.

Using the Source Code

In the code given below, first the shortcut on the Desktop is created, then in the Programs Menu, and then in the Quick Launch bar. We are using System.Environment.GetFolderPath() here. It takes the Special Folder as its parameter. It helps us to know the System folders on the client machine during runtime.

// Create shortcut on Desktop
if(cbDesktop.Checked==true)
{
  File.Move(Application.StartupPath+
     "\\Shortcut to Test.lnk", 
     Environment.GetFolderPath(
     Environment.SpecialFolder.Desktop).Trim()+
     "\\Shortcut to Test.lnk");
}

// Create shortcut in programs menu.
if(cbStartMenu.Checked==true)
{
  File.Move(Application.StartupPath+"\\Test1.lnk",
     Environment.GetFolderPath(
     Environment.SpecialFolder.Programs).Trim()+ 
     "\\Test.lnk");
}

// Create shortcut in Quick Launch Toolbar
if(cbQuickLaunch.Checked==true)
{
  File.Move(Application.StartupPath+
      "\\Test2.lnk",Environment.GetFolderPath(
      Environment.SpecialFolder.ApplicationData)+
      "\\Microsoft\\Internet Explorer\\" + 
      "Quick Launch\\Test.lnk");
}

How to Use This Application in Setup Projects

  1. Add the project output to the file system editor, as shown in the figure below:

    Sample Image - 1.jpg

  2. Select "Primary Output" from the dialog box:

    Sample Image - 2.jpg

  3. The added .exe file will look like this:

    Sample Image - 3.jpg

  4. Right click on the primary output file. And click the Create Shortcut menu:

    Sample Image - 4.jpg

  5. The shortcut file will be created. You can rename this file and use these file names in your actual application, replacing the names of the files in the sample application.

    Sample Image - 5.jpg

  6. In the Solution Explorer, right click on your setup project and select View --> Custom Actions.

    Sample Image - 6.jpg

  7. The Custom Actions window will open:

    Sample Image - 7.jpg

  8. Now, right click on the Install option and click "Add Custom Action":

    Sample Image - 8.jpg

  9. In the dialog box that will open, double click on Application Folder:

    Sample Image - 9.jpg

  10. In the dialog box that will open, click on the "Add File" button:

    Sample Image - 10.jpg

  11. In the dialog box that will open, navigate to the Shortcut.exe file in the Release folder of your application. Only select the .exe file, other dependencies will be added automatically.

    Sample Image - 11.jpg

  12. The Custom Actions Editor will look like this:

    Sample Image - 12.jpg

  13. Select just the added Shortcut.exe, and press F4 or open the Properties window. Set the InstallerClass property to False as shown in the figure below. By default, the InstallerClass property is true.

    Sample Image - 13.jpg

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

Mukund Pujari
India India
Member
*****

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionsome more information about my previous questionmemberpapouAlain5 Jan '06 - 8:30 
How could I generate the *.lnk for the Help.chm, Read Me.txt and My Application.exe and get them in the Shortcut.cs, like in your example.
After that it would be very simple to design a more complete form to accept these little releases...
 
Tx
AnswerRe: some more information about my previous questionmemberMukund Pujari5 Jan '06 - 18:05 
Do you want to create the shortcuts through code. Then this article will be helpful "http://www.codeproject.com/dotnet/shortcut_installer.asp"
GeneralRe: some more information about my previous questionmemberpapouAlain5 Jan '06 - 21:52 
You dont Realy answer to my question.
When looking at Shortcut\Form1.cs code I see the code:
 
if(File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim()+"\\Shortcut to Test.lnk")==false)
{
File.Move(Application.StartupPath+"\\Shortcut to Test.lnk",Environment.GetFolderPath(Environment.SpecialFolder.Desktop).Trim()+"\\Shortcut to Test.lnk");
}

 
1 - I imagine that "Shortcut to Test.lnk" must be in the Shortcut folder ??? or anywhere else ? in this case, where ?
Application.StartupPath refers to the Folder where Shortcut is installed ? Am I right ?
2 - How can I copy the Shortcut created In the File System (SetupSample) into the Shortcut Folder ?
2 - I imagine too, that you renamed the Shortcut "Primary output from sample(Active)" on "Shortcut to Test.lnk", Am I right ?
 
My Question is : If I have a look into the Shortcut folder, I saw the shortcuts (for example "Shortcut to Test.lnk"). But How could I replace Them by the Shortcuts created in the File System (SetupSample) and Be shure they will have the good path to launch my application, the Help of my application and the reading of the Read Me file ?
 
Thank you if you could give me a detailed answer.
GeneralRe: some more information about my previous questionmemberMukund Pujari5 Jan '06 - 22:24 

 
Hi,
 
Here are answers to your question: Let's start with simple ones
 
1) Yes, I have renamed the Shortcut "Primary output from sample(Active)" on "Shortcut to Test.lnk"
 
2) I have not created any folder to copy shortcut. They are left as it is in FileSystemEditor. I just move them to Desktop and Programs Menu and again back to StartUpPath in Release Version.
 
3) Suppose you have application HelloWorld.exe. This application will be installed in your "Program Files" directory by default within the directory probably named "HelloWorld" . In this folder "HelloWorld", there will be *.lnk files. And this itself will be you Application.StartUpPath. I don't have one more Shortcut folder within "HelloWorld" folder.
 
Hope this answers your question Smile | :)
GeneralRe: some more information about my previous questionmemberpapouAlain5 Jan '06 - 22:45 
I give you more informations about my needs...
when deploying my application, I let the user install the application Anywhere He like to...
If I create 3 Shortcuts (My App, Help, Read Me) on the Shortcut Folder and use your Shortcut.exe, called by the Setup as you give the example.
Shortcut will install the Shortcuts on the Desktop, the Start Menu, the Quick Launch, it's ok, it works !
but ! How could I be sure that the Shortcuts really links to the destination the user choosed to install my application if the Path is written in the Shortcut at Design time ?
I need to write the Path in the Shortcut. How ? and where could I find the Path at Deployment time ?
 
Looking forward to reading you...

GeneralRe: some more information about my previous questionmemberMukund Pujari5 Jan '06 - 23:33 

Though you create shortcut at design time, in code you refer to the Application.StartUpPath. So during installation, wherever you install the file that path becomes application startup path. You need not worry about linking part. System will take care of linking. I just tried this, and it works irrespective of where you install it. Smile | :)
AnswerRe: some more information about my previous questionmemberpapouAlain5 Jan '06 - 23:53 
Oh God ! Wink | ;)
 
Thanks for your Help ! Big Grin | :-D
 
Tell me if I'm right now ! Confused | :confused:
If I have understood, it works because the Shortcut.exe is Installed In the application Folder by the Setup.
So the Application.StartupPath is the same for the Setup, the Shortcup.exe and so on (My Help and My ReadMe file, if I decide to install them in the same folder) ???
 
If I'm right, it's a quite simple, but good solution.

GeneralRe: some more information about my previous questionmemberpapouAlain6 Jan '06 - 0:03 
I made a test.Confused | :confused:
On My Desktop I create a shortcut to a file logged on the desktop.
After that I Cut and copy the file in another folder.
If I read the properties of the Desktop, they are not updated by the move operation.
the link still remain to "C:\Documents and Settings\Admin\Desktop" Not in the other Folder Confused | :confused:
So I don't understand what you mean, when writing :
You need not worry about linking part. System will take care of linking. I just tried this, and it works irrespective of where you install it
GeneralRe: some more information about my previous questionmemberMukund Pujari6 Jan '06 - 0:21 
Hi,
 
Don't change the location of original application. Just cut or copy the shortcut somewhere else. It Works.
GeneralRe: some more information about my previous questionmemberMukund Pujari6 Jan '06 - 0:15 

Yes, you are correct. But Application.StartUpPath has no direct significance with respect to Shortcut files. It could be any other valid path. Just to know the source of these *.lnk files at runtime or during installation we have placed them in Application Folder and used Application.StartUpPath. So if you install your application in one folder and *.lnk files in some other folder and change the path in code, still it will work.

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 23 Jan 2006
Article Copyright 2005 by Mukund Pujari
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid