Wix CustomAction C#输入字符串格式错误

时间:2014-11-23 13:20:18

标签: c# wix

我正在尝试为.msi文件实现自定义操作。我想要做的是按照命令提供msi文件:

msiexec /i somefile.msi /l*v output.txt IPADDRESS="127.0.0.1" PORT="9999"

根据以上命令,我想创建一个包含以下内容的文件

{
 "ip":"127.0.0.1",
 "port" : "9999"
}

现在我已经实现了以下可能适用于上述场景的代码:

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

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

            string ipAddress = session["IPADDRESS"];
            string port = session["PORT"];
            string temp = @"
            {
            ""ip"" : ""{0}"" ,
            ""port"" : ""{1}""
            }";
            string config = string.Format(temp, ipAddress, port);
            session.Log("Config Generated was " + config);
            System.IO.File.WriteAllText(@"E:\lpa.config", config);            
            session.Log("Ending WriteFileToDisk");

            return ActionResult.Success;
        }
    }
}

运行我之前写的命令时,我在output.txt中遇到以下错误。

Exception thrown by custom action:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.FormatException: Input string was not in a correct format.
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object args)
   at System.String.Format(IFormatProvider provider, String format, Object args)
   at System.String.Format(String format, Object arg0, Object arg1)
   at SetupCA.CustomActions.WriteFileToDisk(Session session)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
   at Microsoft.Deployment.WindowsInstaller.CustomActionProxy.InvokeCustomAction(Int32 sessionHandle, String entryPoint, IntPtr remotingDelegatePtr)

我在这里做错了什么?请帮忙。

1 个答案:

答案 0 :(得分:2)

您必须在输入字符串中转义{}。为此,您需要添加额外的{}

string temp = @"
{{
    ""ip"" : ""{0}"" ,
    ""port"" : ""{1}""
}}";