18
votes

Is there a way to get an environment variable in WIX into a property?

I'm trying to get the USERPROFILE with:

Property Id="UserFolder"  Value="$(env.USERPROFILE)\EdwardsApp\MyFolder"

But this only picks up the USERPROFILE of the build machine, where the installer is built.

I want it to use the USERPROFILE of the machine where the app is being installed.

4
Using $(env.USERPROFILE) would bring you the value of UserProfile on your build machine while you would be building your Wix installer. Some good solutions are already replied.Farrukh Waheed
In addition to Farrukh's comment: The $(env...) syntax comes from the WiX Preprocessor.CodeFox

4 Answers

19
votes

Alternative is to use SetProperty element - it will effectively create type 51 custom Action. It is simpler than using Custom Action as you don't need to separately specify the schedule for it - everything is done in one element. In my example below, I set the property only if its empty, i.e. was not passed from the command line.

Example:

<SetProperty Id="PROP_MYSOME"
             Before="InstallInitialize" 
             Sequence="execute"
             Value="[%USERDOMAIN]">
    <![CDATA[NOT Installed AND PROP_MYSOME=""]]>
</SetProperty>
11
votes

You can make use of Environment variables during installation but this requires using a custom action. You will need to set the UserFolder property with a Type 51 Custom Action as opposed to setting the property during the build. The [%ENVVARNAME] format is used to make use of an environment variable, but the name of the environment variable is case-sensitive.

A WiX example of a custom action that sets a property:

<CustomAction Id="SetUserFolder" Property="UserFolder" Value="[%USERPROFILE]EdwardsApp\MyFolder" />

You can read more on Custom Actions in WiX here:

http://blogs.technet.com/b/alexshev/archive/2008/02/21/from-msi-to-wix-part-5-custom-actions.aspx

6
votes

since i cant add a comment yet, with regard to @demp's answer, i had to do this to get the condition to evaluate sometime during initialization so that the value could be displayed in a UI dialog:

    <SetProperty Id="MY_PROPERTY" Value="[%USERDOMAIN]" After="LaunchConditions" Sequence="first"  />

I believe that Before="InstallInitialize" happens just before the installation itself proceeds (i.e. copying files and whatnot) and not during the initialization phase of the installer itself.

4
votes

In my case, I'm looking to get the USERPROFILE environment variable of the target machine to install all the files there. I achieved that like:

<Property Id="HOME_FOLDER" >
  <DirectorySearch Id="userProfileSearch" Depth="0" Path="[%USERPROFILE]" />
</Property>

Then all the files went where i wanted them to go.