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

1

22

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.

Rosenberger answered 15/1, 2013 at 14:19 Comment(9)
Can you post the code snippet where you declare the process object and call the start method?Perfume
Why the extra quotes after optional2value? I think your quotes will be unbalanced in the result.Vincenty
As a workaround: Could you add the quotes in the c# program? As a solution, perhaps upgrading to .NET 4.0 ? As another workaround: Make all values mandatory and use a fixed order of input arguments?Winou
If you run a program directly doesn't it change the titlebar of the cmd window to the full path and arguments of whatever you're invoking?Wentz
@DennisJaheruddin based on the matlab tags, it looks like they are trying to launch matlab with some optional command line args, the command line args they are trying to pass require quotes around the option before the value and those quotes are being stripped as they get passed through ProcessStartInfoGoncourt
Regarding the article referenced in the question, there was an update at the top of the article stating it was not a bug and actually a "feature"Goncourt
Hi, sorry I've been AFK for a while. @DJKRAZE I don't really understand how that works, why only a backslash before optional2?Rosenberger
@DennisJaheruddin what TaRDy said is correct, we are using matlab and python 'plugins' for our program, the static c# was just created for testing but thanks for the input :)Rosenberger
@Goncourt Oh yes, I've just seen that update, what a good feature :)Rosenberger
G
26

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
Goncourt answered 15/1, 2013 at 14:49 Comment(4)
Ah it looks like this is working with the test app! I can't try it out with the matlab script until I'm back at work tomorrow but thanks for the glimmer of hope TaRDy :)Rosenberger
Okay I tried it with the Matlab code and it doesn't work for whatever reason. As a workaround we've changed the Matlab code to not use optional arguments but required arguments that the C# app just fills with defaults if not set by the user. Our problem now though is that one of the parameters is a path to an input file but if this path has spaces then Matlab will treat it as 2 separate arguments and therefore...we need the quotes again...this is again messing it up. Has anyone had any problems with running standalone Matlab .exes with quoted arguments?Rosenberger
What a life saver. This was driving me crazy. For those trying to execute a command from PowerShell that takes an argument that includes quotes, the three-quotation trick works, just be sure to escape each of them with a backtick (`).Gynarchy
While this didn't work for me. I used $"app.exe \"{variablename}\"". Thanks for the pointers though.Rosinarosinante

© 2022 - 2024 — McMap. All rights reserved.