Search code examples
c#installationwixwindows-installerwixsharp

How to change the temporary folder used by WixSharp when building an MSI file?


I am using WixSharp v1.9.2.0 to build an MSI file, but when I call the project.BuildMsi() method, WixSharp creates temporary files in the %Temp% folder of Windows. However, my project is quite large, and soon my C drive is full, causing the MSI creation to fail.

Is there a way to change the folder that WixSharp uses for its temporary files? If so, how can I do that?


Solution

  • Wix# using standard Path.GetTempPath & Path.GetTempFileName methods to get location for temp files.

    Per the documentation these methods checks for the existence of environment variables in the following order and returns the first path found:

    1. The path specified by the TMP environment variable.
    2. The path specified by the TEMP environment variable.
    3. The path specified by the USERPROFILE environment variable.
    4. The Windows directory.

    So, you can change the temp folder location by changing TMP environment variable for the project during the build and msi authoring.

    To do so you need to add the following into your wix# csproj (tested for sdk-style project)

    <UsingTask TaskName="SetEnvironmentVariableTask"
               TaskFactory="RoslynCodeTaskFactory" 
               AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
    
      <ParameterGroup>
        <Name ParameterType="System.String" Required="true" />
        <Value ParameterType="System.String" Required="true" />
      </ParameterGroup>
      <Task>
        <Using Namespace="System" />
        <Code Type="Fragment" Language="cs">
          <![CDATA[
          Environment.SetEnvironmentVariable(Name, Value);
        ]]>
        </Code>
      </Task>
    </UsingTask>
    
    <Target Name="SampleTarget" BeforeTargets="MSIAuthoring">
      <SetEnvironmentVariableTask Name="TMP" Value="D:\temp\" />
    </Target>
    

    Here we can see:

    • SetEnvironmentVariableTask inline task which executes the C# code specified inside a Code node
    • SampleTarget target which is called before wix# MSIAuthoring target and calls SetEnvironmentVariableTask to set %tmp% to the D:\temp\.

    Notes:

    • you need to create your new %temp% folder before authoring msi. You can do this manually or add appropriate code into the Main before any wix# actions.
    • this code relies on the wix# target name MSIAuthoring (source) which is subject to change on the wix#-side. So, you need to control this aspect on every wix# update and update your code accordingly.