使用C#的Wix自定义操作

时间:2014-03-20 10:48:08

标签: c# wix

我使用Wix作为我的安装程序和一个包含自定义操作的C#项目。

我想安装一个实用程序(如果它尚未安装),但如果它已经存在于系统上(通过注册表检查),则跳过它的安装。

C#自定义操作:

[CustomAction]
public static ActionResult CheckForUtilityInstallation(Session session)
{

    var UtilityKey = Registry.LocalMachine.OpenSubKey(...);

    if (UtilityKey != null)
    {
        var value = UtilityKey.GetValue("DisplayVersion");
        session.Log("Utility installation found! Version: {0}", value);
        session["UTILITYINSTALLATIONEXISTS"] = "1";
    }
    else
    {
        session.Log("Utility installation not found!");
        session["UTILITYINSTALLATIONEXISTS"] = "0";
    }
    return ActionResult.Success;
}

如果该实用程序存在,则参数UTILITYINSTALLATIONEXISTS将使用参数1传递,否则使用参数0传递。

Wix代码是这样的:

<CustomAction Id="InstallUtility" FileKey="filAAF554748D18441299BE9AEFA324AA76" ExeCommand="/noicons /nofeedback /verysilent /components=Entity" Return="ignore" Execute="deferred" Impersonate="no" />

<CustomAction Id="CheckUtilityInstallation" BinaryKey="CActions.CA.dll" DllEntry="CheckForUtilityInstallation" Return="check"/>

<InstallExecuteSequence>
  <Custom Action="CheckUtilityInstallation" Before="InstallUtility" />
  <Custom Action="InstallUtility" Before="InstallFinalize">UTILITYINSTALLATIONEXISTS = "0"</Custom>
</InstallExecuteSequence>

以某种方式,UTILITYINSTALLATIONEXISTS参数不会从自定义操作C#代码传递给Wix安装程序,从而避免在任何情况下安装实用程序。

我做错了什么?

日志文件:

Calling custom action CActions!CActions.CustomActions.CheckForUtilityInstallation
Utility installation not found!
MSI (s) (54!C0) [11:26:32:917]: PROPERTY CHANGE: Adding UTILITYINSTALLATIONEXISTS property. Its value is '0'.
Action ended 11:26:32: CheckUtilityInstallation.2D89DF12_B7E5_454A_A3A4_884A776A78D1. Return value 1.
MSI (s) (54:64) [11:26:32:932]: Skipping action: InstallUtility.2D89DF12_B7E5_454A_A3A4_884A776A78D1 (condition is false)

1 个答案:

答案 0 :(得分:3)

如果您的C#代码仅用于检查是否存在注册表项,那么最好使用Wix本机方法:

<Property Id="UTILITYINSTALLEDVERSION">
  <RegistrySearch Id="RegSearch1" Name="DisplayVersion" Key="..." Root="HKLM" Type="raw" />
</Property>

然后你的行动顺序为条件:

<Custom Action="InstallUtility" Before="InstallFinalize">UTILITYINSTALLEDVERSION</Custom>

UTILITYINSTALLEDVERSION也将包含实用程序的版本(如果存在)。