Travis Pettry
Travis Pettry

Reputation: 1352

Wix Installer Set Install Location via custom action

I am trying to set the install location via a custom action that looks up the install location via a registry entry. The below custom action finds the correct location to install to. However, I cannot install to that location, my install just goes to the c drive and not the location found by the custom action. How do I set the install location from the custom action?

[CustomAction]
public static ActionResult GetLatestPath(Session session)
{
    session.Log("Begin GetLatestPath");

    string baseKeyPath = @"SOFTWARE\Vendor\Software\2024";
    string latestSubKey = null;
    string dataDir = null;

    try
    {
        for (int i = 99; i >= 0; i--)
        {
            string subKeyVersion = $".{i:00}";
            string fullKeyPath = baseKeyPath + subKeyVersion;

            using (RegistryKey baseKey = Registry.LocalMachine.OpenSubKey(fullKeyPath))
            {
                if (baseKey != null)
                {
                    // Attempt to get the DATADir value
                    object value = baseKey.GetValue("DIR");
                    if (value != null)
                    {
                        latestSubKey = subKeyVersion;
                        dataDir = value.ToString();
                        session.Log($"Found DATADir at: {dataDir} for version: {subKeyVersion}");
                        break; // Exit loop once the latest version is found
                    }
                }
            }
        }

        if (!string.IsNullOrEmpty(dataDir))
        {
            session["INSTALLDIR"] = dataDir;
            session.Log($"Found INSTALLDIRat: {dataDir}");
        }
        else
        {
            session.Log("INSTALLDIR not found.");
            return ActionResult.Failure;
        }
    }
    catch (Exception ex)
    {
        session.Log($"Error retrieving INSTALLDIR: {ex.Message}");
        return ActionResult.Failure;
    }

    session.Log("End GetLatestPath");
    return ActionResult.Success;
}

Now I need to take the location as found by the custom action and use it as the base folder for installing.

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="Software2" Language="1033" Version="24.0.0.0" Manufacturer="Vendor2" UpgradeCode="{guid}">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
        <MediaTemplate EmbedCab="yes" />

        <Property Id="DATADIR" Value="INSTALLDIR" />
        
        <Binary Id="CustomActionDll" SourceFile="..\CustomAction.CA.dll" />

        <CustomAction Id="GetLatestPath" BinaryKey="CustomActionDll" DllEntry="GetLatestPath" Execute="immediate" Return="check" />

        <InstallExecuteSequence>
            <Custom Action="GetLatestPath" 
                    After="InstallInitialize"
                     />
            
        </InstallExecuteSequence>

        <Feature Id="ProductFeature" Title="MDM" Level="1">
            <ComponentGroupRef Id="MyComponents"/>
        </Feature>
    </Product>

    <Fragment>

        <!-- Directory structure -->
        <Directory Id="TARGETDIR" Name="SourceDir">

            <Directory Id="INSTALLDIR" Name="install">
                <Directory Id="PLUGINS" Name="plugins">
                    <Directory Id="INSTALLHERE" Name="MyFolder" />
                </Directory>
            </Directory>
            
        </Directory>
    </Fragment>
</Wix>

I have verified that the custom action does find the correct directory via the installer log.

Upvotes: 0

Views: 48

Answers (0)

Related Questions