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

Dav*_*ing 27 install windows-installer custom-action wix

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

我想将一个参数传递给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>
Run Code Online (Sandbox Code Playgroud)

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

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

}
Run Code Online (Sandbox Code Playgroud)

我原以为能够像这样访问该物业:

string environmentName = session.Property ["ENVIRONMENT"];

但这似乎不起作用.

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

Tom*_*lny 30

如果不是

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

你写这个:

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

那么你将能够像这样引用你的变量:

string env=session.CustomActionData["Environment"];
Run Code Online (Sandbox Code Playgroud)


Dav*_*ing 14

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

呼叫:

msiexec /i ITP.Platform.2.msi ENVIRONMENT=QA CONFIGFILE=EnvironmentConfig.xml
Run Code Online (Sandbox Code Playgroud)

在.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>
Run Code Online (Sandbox Code Playgroud)

使用自定义操作:

    /// <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(',');
    }
Run Code Online (Sandbox Code Playgroud)

作品.

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


jle*_*lew 8

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

  • @SébastienNussbaumer:嗯,那个链接现在已经死了...... jlew:你应该在这里发布实际代码,而不是链接. (3认同)
  • 请注意,博客帖子的链接不再有效.新链接是http://blogs.claritycon.com/sajojacob/2008/02/29/customactiondata-in-wix-with-deferred-custom-actions/ (2认同)

小智 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>
Run Code Online (Sandbox Code Playgroud)

在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"];
    ...
}
Run Code Online (Sandbox Code Playgroud)