Adding a desktop shortcut using Wix

Creating installers is a pain in the ass.

We had to move from ClickOnce to MSI (for reasons bizarre) and we chose Wix (it was easy considering the fact that there was only one option to chose from).

While Wix doesn’t help much in easing the pain (and in some ways it makes things worse), it sure does help in automating the creation of your MSI. While I would like to see a much simpler, automate-able solution for creating MSIs, if you need to do something today Wix is the way to go. Wix is a little complex and unintuitive to use (maybe because of the nature of the underlying technology – MSI). Whatever the reason, I found that creating shortcuts is not as straight forward as one would expect it to be. I would expect to set some attribute to true (like in ClickOnce) and be done with it, but it was not so. Hence this blog post.

The following steps are for creating desktop shortcuts, it differs for creating shortcuts in the Start->Programs list.

First, you need to add the following line

<Directory Id="DesktopFolder" Name="Desktop"/>

Then add a shortcut node under the File node corresponding to the file for which you want the shortcut.

<File Id="StartupExecutable" Name="$(var.StartupExecutable.TargetFileName)" Source="$(var.StartupExecutable.TargetPath)" DiskId="1" KeyPath="yes">
               <Shortcut Advertise="yes"
                         Id="MyProductDesktopShortcut"
                         Directory="DesktopFolder"
                         Name="Name of desktop shortcut"
                         WorkingDirectory="INSTALLLOCATION"
                         Description="Some description"
                         Icon="Icon.exe">
                 <Icon Id="Icon.exe" SourceFile="$(var.StartupExecutable.TargetPath)" />
               </Shortcut>
             </File>

To give a perspective, the whole thing should look like this:

<Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="DesktopFolder" Name="Desktop"/>
      <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="My Company Name"/>
      </Directory>
      <Directory Id="ProgramFilesFolder">
        <Directory Id="MyCompanyFolder" Name="My Company Name">
          <Directory Id="INSTALLLOCATION" Name="My Product Name">
            <Component Id="MyComponent" Guid="1c4ba634-0428-4bcd-ad46-c7cf232e007b">
              <!-- Add our project output dll's -->
              <File Id="StartupExecutable" Name="$(var.StartupExecutable.TargetFileName)" Source="$(var.StartupExecutable.TargetPath)" DiskId="1" KeyPath="yes">
                <Shortcut Advertise="yes"
                          Id="MyProductDesktopShortcut"
                          Directory="DesktopFolder"
                          Name="Name of desktop shortcut"
                          WorkingDirectory="INSTALLLOCATION"
                          Description="Some description"
                          Icon="Icon.exe">
                  <Icon Id="Icon.exe" SourceFile="$(var.StartupExecutable.TargetPath)" />
                </Shortcut>
              </File> 
              <!—Rest of your project assemblies –>
              <File Id="MiddleTier" Name="$(var.MiddleTier.TargetFileName)" Source="$(var.MiddleTier.TargetPath)" DiskId="1" />
              <File Id="Backend" Name="$(var.Backend.TargetFileName)" Source="$(var.Backend.TargetPath)" DiskId="1" />
        </Component>
</Directory>

I hope this was helpful for you fellow journeyman.

2 thoughts on “Adding a desktop shortcut using Wix

  1. Thanks for the post. But I’d be grateful if you could tell me how to place a desktop shortcut to a specific folder which is created by the installer.

    To be more specific, there is a Custom Action program included in the installer which creates a specific folder in the hard drive. It is specified by the user during installation time (say, C:\ABC\logs) and I want the installer to place a desktop shortcut to this folder as well.

    Is it possible?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>