首页 > 解决方案 > 解析作为 exe C# 的进程启动信息参数提供的 json 字符串

问题描述

嗨我有一个应用程序,我需要从另一个 exe 执行。当我作为命令行参数传递时,相同的 json 字符串可以正常工作;但是当我将它作为 Process Start Info Arguments 传递时失败。

命令行参数:

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

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

成功解析

进程启动信息参数:

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

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

解析失败:解析值时遇到意外字符: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;

标签: c#processarguments

解决方案


传递的参数没有被正确转义

它应该被正确转义

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

参考ProcessStartInfo.Arguments 属性


推荐阅读