C#Process Start需要带双引号的参数-它们消失了 [英] C# Process Start needs Arguments with double quotes - they disappear

查看:879
本文介绍了C#Process Start需要带双引号的参数-它们消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Process.Start(ProcessStartInfo);从c#运行cmd line应用程序;

I'm trying to run a cmd line application from c# using Process.Start(ProcessStartInfo);

问题是,cmd行应用程序是独立的matlab .exe,并具有可选参数,这意味着您可以按以下方式在cmd行上传递它们:

The problem is, the cmd line application is a matlab standalone .exe and has optional arguments meaning that you pass them on the cmd line as such:

app.exe "optional1" optional1value "optional2" optional2value

optional1value 是整数或字符串等.

我们遇到的问题是,双引号没有作为"optional1" 参数的一部分传递,因此我相信cmd.exe会得到类似的信息:

The problem we're having is that the double quotes aren't being passed as part of the "optional1" argument and so I believe cmd.exe is getting something like:

app.exe optional1 optional1value optional2 optional2value

或类似的东西,显然是由matlab的解析器所迷惑的.

or something like that, which matlab's parser obviously gets confused by.

我尝试过:

@"""optional1"" optional1value ""optional2" optional2value"""

在其他关于cmd行参数中双引号的问题中,有人建议

,但这似乎对我没有用,

as suggested by some in other SO questions regarding double quotes in cmd line arguments but it doesn't seem to be working for me, nor does:

"\"optional1\" optional1value \"optional2\" optional2value\""

我写了一个小的c#命令行.exe来打印出它得到的参数.如果我将命令行参数放在VS Project-> Debug区域中并运行它,则它将用双引号将它们打印出来,但是由于所有转义等原因,因此当我在代码中执行此操作时,.exe将不带任何双引号的情况下打印自变量.

I have written a small c# command line .exe to print out the arguments it gets. If I put the command line arguments in VS Project->Debug area and run it then it prints them with double quotes but because of all the escaping etc. when I do this in code, the .exe prints the arguments without any double quotes.

我发现了这篇文章关于它可能是.NET 3.5的cmd解析器中的错误,但似乎无法找到可行的解决方案.

I found this article about it maybe being a bug in .NET 3.5's cmd parser but can't seem to find a viable solution.

有人有什么想法吗?

感谢您的时间,

雨披

P.S.当使用Process.Start()发送参数时,是否有办法查看cmd.exe会得到什么?我让我的进程打开一个cmd窗口,但是很高兴看到cmd.exe这行显示为:"app.exe optional1 optional1value"等.

P.S. Is there a way to see what cmd.exe gets when sending arguments over with Process.Start()? I have my Process opening a cmd window but it would be nice to see the line cmd.exe gets such as: "app.exe optional1 optional1value" etc.

再次感谢.

推荐答案

ProcessStartInfo.Arguments中的引号必须转义为三个引号(""").这是因为单引号用于传递包含空格作为单个参数的字符串.

Quotes in ProcessStartInfo.Arguments must be escaped as three quotes ("""). This is because a single quote is used for passing a string containing spaces as a single argument.

在此处查看文档:

所有cmd_app.exe所做的工作就是宣布其args数量和args是什么,并通过此输入显示:

All cmd_app.exe does is announce its # of args and what the args are, with this input it displays:

"optional1"
optional1value
"optional2"
optional2value

这篇关于C#Process Start需要带双引号的参数-它们消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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