Adam Short
Adam Short

Reputation: 95

Default install path to ProgramFilesFolder without specifying ProgramFilesFolder in wxs

I currently have a legacy Visual Studio Install Projects project that creates an MSI. With this I can specify "TARGETDIR="somepath"" on the command line and have it install to "somepath". Now with WIX, if I don't specify ProgramFilesFolder in my wxs, "TARGETDIR" still works, however in my UI of the installer the default path is "C:\Manufacturer\Product" whereas I still want it to default to ProgramFilesFolder. Having support of "TARGETDIR" is necessary to also support upgrading to the legacy MSI from within the app itself.

I have found some ways to change the UI default directory to ProgramFilesFolder, however TARGETDIR doesn't change to this directory (or a directory the user specifies) so it still installs to C:\Manufacturer\Product.

Does anyone have any ideas here? I'm guessing some kind of custom action will do it but I feel like I've tried most of the suggestions such as:

<Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLDIR" Name="blah">

As mentioned, I must be able to use "TARGETDIR" and not "INSTALLDIR" (although that works).

If I really have to use "INSTALLDIR" then I can make it work but it makes maintaining the legacy MSI and the WiX one tricky due to the nature of how they're used.

Edit

Solution: <Custom Action="SetINSTALLDIR" Before="AppSearch">Not Installed</Custom> in both InstallExecuteSequence and InstallUISequence.

This points to: <CustomAction Id="SetINSTALLDIR" BinaryKey="CustomActionsBinary" DllEntry="SetInstallDir" />

SetInstallDir is the following:

[CustomAction]
public static ActionResult SetInstallDir(Session session)
{
    TraceLogger.yRTraceInfo(nameof(SetInstallDir));
    string installDir = session["APPLICATIONFOLDER"];
    string targetDir = string.Empty;

    try
    {
        targetDir = session["TARGETDIR"];
    }
    catch (Exception e)
    {
        Console.log(e.Message);
    }

    if (string.IsNullOrEmpty(installDir) && !string.IsNullOrEmpty(targetDir))
    {
        session["APPLICATIONFOLDER"] = targetDir;
        console.log($"Setting APPLICATIONFOLDER to {targetDir}");
    }
    return ActionResult.Success;
}    

Upvotes: 0

Views: 501

Answers (1)

Christopher Painter
Christopher Painter

Reputation: 55571

I suppose you could have a SetProperty custom action to assign INSTALLDIR a value if it is empty and TARGETDIR has a value and Not Installed. Schedule it early on in both the Install UI and Install Execute sequences ahead of AppSearch.

FYI in WiX INSTALLLOCATION is more commonly used. INSTALLDIR is more of an InstallShield thing and TARGETDIR a Visual Studio thing.

<SetProperty Id="INSTALLDIR" Value="[TARGETDIR]" Before="AppSearch">Not INSTALLDIR and TARGETDIRDIR and Not Installed</SetProperty> 

Upvotes: 2

Related Questions