如何正确启动以json字符串作为参数的.exe? [英] How do I start a .exe with a json string as parameter correctly?

查看:586
本文介绍了如何正确启动以json字符串作为参数的.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面仅是该功能的一个示例.

The below is just an example of the functionality.

我有一个这样的模型:

public class StartParams
{
    public string ParameterOne { get; set; }
    public string ParameterTwo { get; set; }
    public string ParameterThree { get; set; }
}

从WPF应用程序.我像这样将其序列化为JSON:

From a WPF app. I am serializing it as JSON like this:

var startParams = new StartParams
{
    ParameterOne = "parameterOne",
    ParameterTwo = "parameterTwo",
    ParameterThree = "parameterThree"
};

var jsonStartParams = JsonConvert.SerializeObject(startParams);

然后,我将使用JSON字符串作为参数启动.exe文件.

Then I'm launching a .exe file with the JSON string as a parameter.

ProcessStartInfo info = new ProcessStartInfo
{
    Arguments = jsonStartParams,
    FileName = "C:\\Folder\\File.exe"
};

Process.Start(info);

File.exe 中,我有一个包含字符串的任务:

In File.exe I have a Task which takes a string:

public static async Task DoSomething(string jsonStartParams)
{
    var startParams = JsonConvert.DeserializeObject<StartParams>(jsonStartParams);

调试时,我可以像这样在File.exe中调用静态Main方法:

When debugging, I can call the static Main method in File.exe like this:

string[] parameters = {jsonStartParams};
File.Program.Main(parameters);

这就像一个咒语,但是一旦我用JSON字符串参数使用Process.Start调用.exe文件,它就会失败并显示

This works like a charm, but as soon as I call the .exe file with Process.Start, with the JSON string parameter, it fails with

Newtonsoft.Json.JsonReaderException

Newtonsoft.Json.JsonReaderException

在JSON对象的第一个道具上.

at the first prop in the JSON object.

有人可以为我指出解决方案的正确方向吗?

Can someone please point me in the right direction of a solution?

谢谢!

推荐答案

jsonStartParams中的引号使参数分成多个参数.尝试这样转义引号:

The quotes in jsonStartParams are making the argument separated into multiple arguments. Try escaping the quotes like this:

ProcessStartInfo info = new ProcessStartInfo
{
    Arguments = "\"" + jsonStartParams.Replace("\"",  "\\\"") + "\"",
    FileName = "C:\\Folder\\File.exe"
};

这篇关于如何正确启动以json字符串作为参数的.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆