Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Desktop Shortcut to an existing FOLDER using WiX

I have the need to create a Desktop Shortcut to an existing FOLDER (NOT to a file) using Wix. To elaborate more, my installer program has a CustomAction program written using C# associated with it. This CustomAction program creates a folder named "BSS" of which the path is selected by user.

C:\ProgramData\MT\BSS

Now I need to place a Desktop Shortcut to this folder using WiX. However, I encounter a problem since this folder does not have a folder structure within WiX. The closest code I could find was the following.

<Directory Id="DesktopFolder" Name="Desktop"/>
  <Directory Id="CommonAppDataFolder" Name="ProgramDataFolder"/>
  <Component Id="ComponentBSStrageShortcut" Guid="{8436995c-2e76-4030-b92d-c6b4bc243c43}">
    <Shortcut Id="ShortcutBSStrageShortcut"
              Directory="DesktopFolder"
              WorkingDirectory="APPLICATIONFOLDER"
              Target="[CommonAppDataFolder]/MTK/BSStrage"
              Name="BSStrage"
              Show="normal"/>
    <RegistryValue Action="write"
                   Key="SOFTWARE/MTK/BackStreet"
                   Root="HKCU"
                   Type="string"
                   KeyPath="yes"
                   Value="ApplicationFolderName"/>
  </Component>

When I build the installer this way, it actually creates a shortcut on Desktop. However, WiX seems to think that BSStrage is a file/application so it places a shortcut to an imaginary application called BSStrage in the location C:\ProgramData\MT. But double clicking on it dosen't help as there is no program that can be used to open it.

Obviously I'm doing it wrong here. Can someone please help me with this, so as how to overcome this problem. Note that I'm extremely new to Wix (it's been only two days) and has never worked with it before. Any code sample would be of great help.

like image 857
Sach Avatar asked Apr 22 '10 08:04

Sach


3 Answers

I just ran into the same problem; it seems that creating a Shortcut tag with a Target attribute of the form
Target="[CommonAppDataFolder]" works fine, but trying to append subdirectories such as
Target="[CommonAppDataFolder]\MTK\BSStrage" results in the creation of a shortcut that doesn't work.

Fortunately, I've discovered a solution. The trick is to create a hierarchy of Directory tags leading to the directory that you want to create a shortcut to, which then contains a Component tag containing a Shortcut tag, like so:

<Directory Id="DesktopFolder" Name="Desktop"/>
<Directory Id="CommonAppDataFolder" Name="ProgramDataFolder">
  <Directory Id="AppDataMTK" Name="MTK">
    <Directory Id="AppDataBSStrage" Name="BSStrage">
      <Component Id="ComponentBSStrageShortcut" Guid="{8436995c-2e76-4030-b92d-c6b4bc243c43}">
        <CreateFolder/>
        <Shortcut Id="ShortcutBSStrageShortcut"
                  Directory="DesktopFolder"
                  Name="BSStrage""/>
      </Component>
    </Directory>
  </Directory>
</Directory>

Note that the target directory has to actually exist at the time the shortcut is created, or else you'll end up with the same problem: a broken shortcut. This is why I added the <CreateFolder/> tag inside the Component tag, to create the directory on install.

like image 124
Karl von L Avatar answered Oct 01 '22 01:10

Karl von L


The slashes in your Shortcut/@Target should be backslashes. Explorer is probably interpreting your shortcut as "Launch CommonAppDataFolder with switches /MTK and /BSStrage". At least, that's my first guess.

like image 41
Rob Mensching Avatar answered Oct 01 '22 03:10

Rob Mensching


I changed my requirements a bit and got the code to work as follows. Change being now I create a shortcut to the ProgramData folder.

<!-- Desktop Shortcut --> 
  <Directory Id="DesktopFolder" Name="Desktop"/> 
  <Directory Id="CommonAppDataFolder" Name="ProgramDataFolder"/> 
  <Component Id="MTDesktopShortcut" Guid="{8436995c-2e76-4030-b92d-c6b4bc243c43}"> 
    <Shortcut Id="MTShortcut" 
              Directory="DesktopFolder" 
              WorkingDirectory="APPLICATIONFOLDER" 
              Target="[CommonAppDataFolder]" 
              Name="MT" 
              Show="normal"/> 
    <RegistryValue Action="write" 
                   Key="SOFTWARE/MT/BS" 
                   Root="HKCU" 
                   Type="string" 
                   KeyPath="yes" 
                   Value="ApplicationFolderName"/> 
  </Component> 

It works fine and creates the shortcut fine. However there is one problem since it creates the shortcut on AllUsers Desktop while I want it to be created on the Current User's desktop. What change should I do?

Also note that my installer performs a all-user install, and I'm not at liberty to change that. I merely needs a way to create this shortcut on Current User's desktop while the installer can still do a all-user install.

like image 26
Sach Avatar answered Oct 01 '22 02:10

Sach