Wix 自定义操作 C# 输入字符串格式不正确错误

本文关键字:格式 不正确 错误 字符串 输入 自定义 操作 Wix | 更新日期: 2023-09-27 18:31:31

我正在尝试为.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)

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

Wix 自定义操作 C# 输入字符串格式不正确错误

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

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