有没有办法确定 WiX 中应用程序先前版本的安装位置?

问题描述 投票:0回答:1

我正在迁移应用程序的安装位置。 我需要将一些文件从旧位置复制到新位置。卸载期间不会删除旧位置,因此我们对此表示同意。 我计划使用 C# CustomAction 来实现此目的,并且我能够使用以下方式识别是否安装了特定版本的应用程序:

<Upgrade Id="$(var.TOOLBOX_PRODUCT_UPGRADECODE)">
      <UpgradeVersion OnlyDetect="yes" Property="SELFFOUND"
                      Minimum="3.2.1" IncludeMinimum="yes"
                      Maximum="3.7.1" IncludeMaximum="yes" />
</Upgrade>

<InstallExecuteSequence>
  <Custom Action="LaunchReadme" After="InstallFinalize">VIEWRELEASENOTES = 1 and NOT Installed </Custom>
  <Custom Action="CopyFiles" After="InstallFinalize">SELFFOUND</Custom>
</InstallExecuteSequence>

<CustomAction Id="CopyFiles" BinaryKey="CustomActions" Execute="immediate" DllEntry="ImmediateCopyCustomAction" />

我唯一缺少的是能够检测以前的安装位置。这可以使用产品 ID 来完成吗?

wix
1个回答
0
投票

在自定义操作中直接使用产品 ID 检测以前的安装位置并不简单,因为产品 ID 不存储有关安装路径的信息。

您可以使用Windows Installer API查询存储应用程序安装路径的属性。通常,这是 INSTALLLOCATION 属性,但它可能会有所不同,具体取决于安装程序的设置方式。

使用 Windows Installer API

using System;
using Microsoft.Deployment.WindowsInstaller;

public class CustomActions
{
    [CustomAction]
    public static ActionResult FindPreviousInstallationPath(Session session)
    {
        try
        {
            string productCode = "your previous prod code goes hereYOUR_PREVIOUS_PRODUCT_CODE_HERE"; 
            string property = "InstallLocation"; 
            string installLocation = Registry.GetValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\" + productCode + @"\InstallProperties", "InstallLocation", null) as string;
            if (!string.IsNullOrEmpty(installLocation))
            {
                session.Log("Previous installation location found: " + installLocation);
                session["PREV_INSTALL_PATH"] = installLocation;
                return ActionResult.Success;
            }
            else
            {
                session.Log("Previous installation location not found.");
                return ActionResult.Failure;
            }
        }
        catch (Exception ex)
        {
            session.Log("Error finding previous installation location: " + ex.Message);
            return ActionResult.Failure;
        }
    }
}

此示例尝试从注册表中读取 InstallLocation,注册表是存储此类信息的常见位置。请注意,您需要调整产品代码以及可能的属性名称以适合您的特定应用程序

© www.soinside.com 2019 - 2024. All rights reserved.