c# - 解析作为 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;
解决方案
传递的参数没有被正确转义
它应该被正确转义
var jsonString = "{\"mydllpath\":\"D:\dll\",\"FilePath\":\"D:\Input\abc.doc\", \"Attribute\":\"word\"}";
var args = string.Format("\"\"\"{0}\"\"\"", jsonString);
psi.Arguments = args;
//...
推荐阅读
- javascript - 在向机器人触发消息之前,不会显示问候消息
- postman - 无法从邮递员调用 API 并使用多部分表单数据用值数组填充键
- java - Mapstruct 插件 IDEA 支持
- sql - PostgreSQL 计算的 JSON 值索引在 peewee 中不起作用?
- sql - SQL函数将日期四舍五入到最近的月份
- amazon-web-services - AWS Cognito:如何在用户注册时触发 lambda 以附加 IoT 策略?
- java - 受垃圾收集影响的时间关键处理程序循环
- elasticsearch - 在 Elasticsearch 中更改映射字段结构流程
- python - 我的 Python 挑战逻辑有什么问题?
- pytorch - 用于训练的线性变换输出和地面实况的损失