WiX Toolset如何使用C#customaction传递三个参数来设置属性

时间:2015-04-07 21:47:28

标签: c# wix windows-installer

我是C#和WiX相对新手

我的目标是传递三个参数(InputString,SearchString,ReplaceString),以便转换在erlang(erl.ini)文件中使用的路径,这需要Windows中的双反斜杠。

我希望能够访问OutputString以在我的WiX项目中设置属性。

这是我的C#习惯代码

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;

namespace DGCustomActions
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CASearchAndReplace(Session session)
        {

            try
            {
                session.Log("Begin CASearchAndReplace");

                string InputString = session.CustomActionData["InputString"];
                string SearchString = session.CustomActionData["SearchString"];
                string ReplaceString = session.CustomActionData["ReplaceString"];

                session["OutputString"] = InputString.Replace(SearchString, ReplaceString);

                session.Log("CASearchAndReplace Successful");
            }
            catch (Exception ex)
            {
                session.Log("ERROR in custom action CASearchAndReplace: {0}", 
                            ex.ToString());
                return ActionResult.Failure;
            }

            return ActionResult.Success;
        }
    }
}

这是我尝试传递参数和运行习惯

    <CustomAction Id='PassValuesErlangBindir'
                  Execute='immediate'
                  Property='TransformErlangBindir'
                  Value='InputString=[ERLANGERTSBINDIR];SearchString=\;ReplaceString=\\' />

    <CustomAction Id='TransformErlangBindir'
                  BinaryKey='DGCustomActions'
                  DllEntry='CASearchAndReplace'
                  Execute='deferred'
                  Return='check' />

我目前没有访问OutputString属性的代码

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

在仔细阅读之前发布的Rolo's(感谢您的意见)后,我做了以下更改,这就是诀窍。

注意:如果有人提出可以使我的解决方案更清洁的建议,我很感激您的回复。

这是我更新的C#惯例代码

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;

namespace DGCustomActions
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult SearchAndReplace(Session session)
        {
            session.Log("Begin SearchAndReplace");

            string InputString = session["InputString"];
            string SearchString = session["SearchString"];
            string ReplaceString = session["ReplaceString"];

            session["OutputString"] = InputString.Replace(SearchString, ReplaceString);

            session.Log("SearchAndReplace Successful");

            return ActionResult.Success;
        }
    }
}

这是我更新的一套习惯

<CustomAction  Id="Set.SearchString" Property="SearchString" Value="\" />
<CustomAction  Id="Set.ReplaceString" Property="ReplaceString" Value="\\" />
<CustomAction  Id="Set.OutputString" Property="OutputString" Value="nada" />

<CustomAction  Id="Set.ErlangBindir.InputString" Property="InputString" Value="[ERLANGDIR]erts-$(var.ErlangVersion)\bin" />

<CustomAction Id="Transform.ErlangBindirString"
              BinaryKey="DGCustomActions"
              DllEntry="SearchAndReplace"
              Execute="immediate"
              Return="check" />

<CustomAction  Id="Set.ErlangBindir.OutputString" Property="ErlangBindir" Value="[OutputString]" />

<CustomAction  Id="Set.ErlangRootdir.InputString" Property="InputString" Value="[ERLANGDIR]bin" />

<CustomAction Id="Transform.ErlangRootdirString"
              BinaryKey="DGCustomActions"
              DllEntry="SearchAndReplace"
              Execute="immediate"
              Return="check" />

<CustomAction  Id="Set.ErlangRootdir.OutputString" Property="ErlangRootdir" Value="[OutputString]" />

这是我更新的安装执行顺序

<InstallExecuteSequence>
    <!-- Run after InstallInitialize: Sequence="1500" -->
    <Custom Action="Set.SearchString" Sequence="1501" />
    <Custom Action="Set.ReplaceString" Sequence="1502" />
    <Custom Action="Set.OutputString" Sequence="1503" />
    <Custom Action="Set.ErlangBindir.InputString" Sequence="1504" />
    <Custom Action="Transform.ErlangBindirString" Sequence="1505" />
    <Custom Action="Set.ErlangBindir.OutputString" Sequence="1506" />
    <Custom Action="Set.ErlangRootdir.InputString" Sequence="1507" />
    <Custom Action="Transform.ErlangRootdirString" Sequence="1508" />
    <Custom Action="Set.ErlangRootdir.OutputString" Sequence="1509" />
</InstallExecuteSequence>