You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过自定义操作设置Wix Installer的安装路径?

如何通过自定义操作设置Wix Installer的安装路径

我已经实现了读取注册表获取安装路径的自定义操作,日志确认能正确找到目标目录,但安装时仍默认指向C盘,而非自定义操作获取的路径。以下是我的代码:

自定义操作C#代码

[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)
                {
                    // 尝试获取DATADir值
                    object value = baseKey.GetValue("DIR");
                    if (value != null)
                    {
                        latestSubKey = subKeyVersion;
                        dataDir = value.ToString();
                        session.Log($"Found DATADir at: {dataDir} for version: {subKeyVersion}");
                        break; // 找到最新版本后退出循环
                    }
                }
            }
        }

        if (!string.IsNullOrEmpty(dataDir))
        {
            session["INSTALLDIR"] = dataDir;
            session.Log($"Found INSTALLDIR at: {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;
}

当前Wix配置

<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 Id="TARGETDIR" Name="SourceDir">

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

解决方案

问题出在自定义操作的执行时机太晚:InstallInitialize是安装执行序列中开始执行实际安装操作的节点,此时Windows Installer已经完成了目录成本计算(CostInitialize阶段),修改INSTALLDIR不会生效。需要将自定义操作移到成本计算之前执行,同时覆盖UI序列和执行序列,确保无人值守安装也能生效。

修改步骤:

  1. 调整自定义操作的执行顺序
    在Product节点下添加InstallUISequence,并修改InstallExecuteSequence,将自定义操作移到CostInitialize之前:

    <InstallUISequence>
        <!-- UI序列中在成本计算前执行,确保用户看到正确的默认路径 -->
        <Custom Action="GetLatestPath" Before="CostInitialize" />
    </InstallUISequence>
    
    <InstallExecuteSequence>
        <!-- 执行序列中同样在成本计算前执行,确保无人值守安装生效 -->
        <Custom Action="GetLatestPath" Before="CostInitialize" />
    </InstallExecuteSequence>
    
  2. 验证属性设置
    自定义操作中设置session["INSTALLDIR"]的逻辑是正确的,确保注册表读取正常即可。如果需要允许用户在UI中修改路径,无需额外配置,Wix会自动处理属性的覆盖。

修改后,Windows Installer会在计算磁盘空间和目录路径前获取注册表中的INSTALLDIR值,安装时就会使用该路径作为默认安装目录。

内容的提问来源于stack exchange,提问作者Travis Pettry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 13:24:50