Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Process Start needs Arguments with double quotes - they disappear

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

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

Where optional1value is a integer or string etc.

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

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

I have tried:

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

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\""

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.

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.

Does anyone have any ideas?

Thank you for your time,

Poncho

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.

Thanks again.

like image 922
poncho Avatar asked Jan 15 '13 14:01

poncho


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is -= in C?

-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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.

See the documentation here.

var psi = new ProcessStartInfo(
    "cmd_app.exe",
    "\"\"\"optional1\"\"\" optional1value \"\"\"optional2\"\"\" optional2value");
Process.Start(psi);

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

"optional1"
optional1value
"optional2"
optional2value
like image 126
TaRDy Avatar answered Oct 07 '22 18:10

TaRDy