如何将msiexec属性传递给WiX C#自定义操作?

时间:2009-05-07 16:11:58

标签: wix windows-installer install custom-action

我有一个用Wxs 3.0创建的MSI文件。我的MSI引用了一个C#自定义操作,使用新的C# Custom Action project编写。

我想将一个参数传递给msiexec,该参数被路由到我的自定义操作 - 例如:

msiexec / i MyApp.msi ENVIRONMENT = TEST#

在我的.wxs文件中,我引用了我的自定义操作:

<Property Id="ENVIRONMENT"/>
<Binary Id="WixCustomAction.dll"  SourceFile="$(var.WixCustomAction.Path)" />
<CustomAction Id="WixCustomAction" BinaryKey="WixCustomAction.dll"    DllEntry="ConfigureSettings"/>
<InstallExecuteSequence>
   <Custom Action="WixCustomAction" After="InstallFiles"></Custom>
</InstallExecuteSequence>

我的C#自定义操作设置如下:

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

}

我原本希望能够像这样访问该物业:

string environmentName = session.Property [“ENVIRONMENT”];

但这似乎不起作用。

如何访问我在自定义操作中传递给msiexec的属性?

5 个答案:

答案 0 :(得分:30)

如果不是

<CustomAction Id="SetCustomActionDataValue"
              Return="check"
              Property="Itp.Configurator.WixCustomAction"
              Value="[ENVIRONMENT],G2,[CONFIGFILE],[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" />
你写下这个:

<CustomAction Id="SetCustomActionDataValue"
              Return="check"
              Property="Itp.Configurator.WixCustomAction"
              Value="Environment=[ENVIRONMENT];G=G2;ConfigFile=[CONFIGFILE];TargetDir=[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" />

然后你就可以像这样引用你的变量了:

string env=session.CustomActionData["Environment"];

答案 1 :(得分:14)

只是为了完整;使用Jeremy Lew描述的方法,在上面的博客中允许以下内容:

通话:

msiexec /i ITP.Platform.2.msi ENVIRONMENT=QA CONFIGFILE=EnvironmentConfig.xml

在.wxs文件中使用:

<Property Id="ENVIRONMENT" Secure="yes" />
<Property Id="CONFIGFILE" Secure="yes" />
<Binary Id="Itp.Configurator.WixCustomAction.dll"
        SourceFile="$(var.Itp.Configurator.WixCustomAction.Path)" />

<CustomAction Id="SetCustomActionDataValue"
              Return="check"
              Property="Itp.Configurator.WixCustomAction"
              Value="[ENVIRONMENT],G2,[CONFIGFILE],[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" />

<CustomAction Id="Itp.Configurator.WixCustomAction"
              Return="check"
              Execute="deferred"
              BinaryKey="Itp.Configurator.WixCustomAction.dll"
              DllEntry="ConfigureItpBrandSettings" />

<InstallExecuteSequence>
  <Custom Action="SetCustomActionDataValue" After="InstallFiles"></Custom>
  <Custom Action="Itp.Configurator.WixCustomAction" After="SetCustomActionDataValue"></Custom>
</InstallExecuteSequence>

使用自定义操作:

    /// <summary>
    /// CustomAction keys should be Environment,BrandId,ConfigPath,itpBasePath
    /// </summary>
    /// <param name="session"></param>
    /// <returns></returns>
    [CustomAction]
    public static ActionResult ConfigureItpBrandSettings(Session session)
    {
        string[] arguments = GetCustomActionDataArguments(session);

        string environmentName = arguments[0];
        string brandId = arguments[1];
        string configPath = arguments[2];
        string itpBasePath = arguments[3];

        //Do stuff

        return ActionResult.Success;
    }

    private static string[] GetCustomActionDataArguments(Session session)
    {
        string[] keys = new string[session.CustomActionData.Keys.Count];
        session.CustomActionData.Keys.CopyTo(keys,0);
        return keys[0].Split(',');
    }

作品。

解析CustomActionData参数非常难看,但确实有效。希望有人知道更优雅的方式来做到这一点。

答案 2 :(得分:8)

您的自定义操作需要是延迟的自定义操作才能在InstallFiles之后运行。延迟的自定义操作无权访问属性,但可以访问CustomActionData。有关如何解决该问题的讨论,请参阅this blog post。 (此示例是VBScript自定义操作,但您可以通过session.CustomActionData集合检索值。)

答案 3 :(得分:8)

这是我的工作代码:

<Binary Id="MyCA" SourceFile="..\bin\ChainerRun.CA.exe" />

<CustomAction Id="SetCustomActionDataValue" Return="check" Property="CustomActionData" Value="TARGETDIR=[TARGETDIR];AA=Description;" />

<CustomAction Id="ReadAndSet" 
            BinaryKey="MyCA" 
            DllEntry="ReadAndSet" 
            Execute="immediate"
            HideTarget="no" 
            Return="check" />

<InstallExecuteSequence>
    <Custom Action="SetCustomActionDataValue" Before="InstallFiles" />
    <Custom Action="ReadAndSet" After="SetCustomActionDataValue" />
</InstallExecuteSequence>

在C#自定义动作函数中:

[CustomAction]
public static ActionResult ReadAndSet(Session session)
{
    ActionResult retCode = ActionResult.NotExecuted;

    System.Diagnostics.Debug.Assert(false);

    session.Log("ReadAndSet() begins ...");

    string installLocation = session.CustomActionData["TARGETDIR"];
    string hostName = session.CustomActionData["AA"];
    ...
}

答案 4 :(得分:0)

如果我们谈论Wix Sharp(而不是简单的Wix及其XML内容),添加自定义属性是件小事。您所要做的就是为您的托管操作设置 UsesProperties 属性。

例如,如果要添加名为“ MYPROP ”的自定义属性,只需按以下方式定义您的操作:

new ElevatedManagedAction(nameof(CustomActions.MyCustomAction))
{
    Condition = Condition.Installed,
    When = When.Before,
    Step = Step.RemoveFiles,
    Return = Return.check,
    Execute = Execute.deferred,
    UsesProperties = "MYPROP"
}

通过msiexec命令行设置属性值:

msiexec /i my.msi MYPROP=MYVALUE

然后您就可以通过自定义操作访问它了:

[CustomAction]
public static ActionResult MyCustomAction(Session session)
{
    session.Log("MYPROP VALUE: " + session.CustomActionData["MYPROP"]);
    return ActionResult.Success;
}

如果未通过命令行设置属性,则默认值为空字符串。