Parse json string provided as Process Start Info Arguments for exe C#
Asked Answered
B

4

5

Hi I have an application which i need to execute from another exe. The same json string works fine when i pass as command line arguments; but fails when i pass it as Process Start Info Arguments.

command line arguments:

Input (i.e. args[0]): "{\"mydllpath\":\"D:\\dll\",\"FilePath\":\"D:\\Input\\abc.doc\", \"Attribute\":\"word\"}"

Console.Writeline: {"mydllpath":"D:\\dll","FilePath":"D:\\Input\\abc.doc", "Attribute":"word"}

Success parsing

Process Start Info Arguments:

Input: "{\"mydllpath\":\"D:\\dll\",\"FilePath\":\"D:\\Input\\abc.doc\", \"Attribute\":\"word\"}"

Console.Writeline:{"mydllpath":"D:\dll","FilePath":"D:\Input\abc.doc", "Attribute":"word"}

Failed parsing: Unexpected character encountered while parsing value: D.

ProcessStartInfo psi = new ProcessStartInfo("D:\\ETS\\AE\\bin\\Debug\\AE.exe");
string json = "{\"mydllpath\":\"D:\\dll\",\"FilePath\":\"D:\\Input\\abc.doc\", \"Attribute\":\"word\"}";
psi.Arguments = json;
Process p = new Process();
Debug.WriteLine(psi.FileName + " " + psi.Arguments);
p.Start();
p.StartInfo = psi;
Brocket answered 18/9, 2018 at 13:57 Comment(3)
Include the code used to make the process call.Avelar
For clarification, the above JSON in the code, why are you escaping the slashes in the paths?Avelar
It gives error without the escape sequenceBrocket
P
4

You need to wrap the argument with "", and escape '"' symbol by replacing it with '""'.

Here is a sample code to start the application with the JSON command line argument

    var argumentString = JsonSerializer.Serialize(new
    {
        DeviceId = _deviceInfo?.DeviceId,
        ApplicationStartUrl = _deviceInfo?.AppStartUrl
    }).Replace("\"", "\"\"");
    argumentString = $"\"{argumentString}\""; 
 
    var processStartupInfo = new ProcessStartInfo(Path.Combine("Application",
_configuration.Executable ?? ""), argumentString);
    processStartupInfo.WorkingDirectory = Path.Combine(Assembly.GetExecutingAssembly().Location, "Application");
    _applicationProcess = Process.Start(processStartupInfo);
Photic answered 11/1, 2023 at 8:46 Comment(0)
A
2

The passed arguments is not being escaped properly

it should be properly escaped

var jsonString = "{\"mydllpath\":\"D:\dll\",\"FilePath\":\"D:\Input\abc.doc\", \"Attribute\":\"word\"}";
var args = string.Format("\"\"\"{0}\"\"\"", jsonString);
psi.Arguments = args;
//...

Reference ProcessStartInfo.Arguments Property

Avelar answered 18/9, 2018 at 14:10 Comment(5)
Error!! "Bad JSON escape sequence"Brocket
@Brocket alright I'll remove my comments and update answer so you can see what I mean.Avelar
It still gives error "unrecognized escape sequence" in VS editorBrocket
@Brocket are you referring to runtime error or syntax error when you refer to VS editor error? That shoulds more like a syntax errorAvelar
@Brocket take a look at the following docs learn.microsoft.com/en-us/dotnet/api/…Avelar
G
1

so many years no answer... I encounter the problem and finally got is work with a tricky way, it's a node process in my case. Hope it can help.

  1. Replace " to single quote ' in the parameter in .net side (because process argument will take " as no quote, so your process will receive a json string without quote!!!)

    argumentStr= argumentStr.Replace(""", "'"); proc.StartInfo.Arguments = $"app.js "{argumentStr}""

  2. Replace ' with " in node side

    let arg= process.argv[2]; arg = arg.replace(/'/g, '"'); const yourObject = JSON.parse(arg);

Gregson answered 29/7, 2022 at 8:20 Comment(0)
N
0

Only thing that worked for me was to convert to base64, and then convert back afterwards

string args = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)));

            using (Process process = new Process())
            {
               process.StartInfo = new ProcessStartInfo()
               {
                  FileName = Assembly.GetExecutingAssembly().Location,
                  Arguments = args,
                  UseShellExecute = false,
                  CreateNoWindow = true
               };

               process.Start();
               process.WaitForExit(timeoutSeconds * 1000);
            }
byte[] bytes = Convert.FromBase64String(args);
         string requestStr = System.Text.Encoding.UTF8.GetString(bytes);
         ReportRequest request = JsonConvert.DeserializeObject<ReportRequest>(requestStr);
Narah answered 9/12, 2021 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.