How do I launch a subprocess in C# with an argv? (Or convert agrv to a legal arg string)

Posted by lucas on Stack Overflow See other posts from Stack Overflow or by lucas
Published on 2010-06-02T17:42:35Z Indexed on 2010/06/02 17:44 UTC
Read the original article Hit count: 269

Filed under:
|
|
|
|

I have a C# command-line application that I need to run in windows and under mono in unix. At some point I want to launch a subprocess given a set of arbitrary paramaters passed in via the command line. For instance:

Usage: mycommandline [-args] -- [arbitrary program]

Unfortunately, System.Diagnostics.ProcessStartInfo only takes a string for args. This is a problem for commands such as:

./my_commandline myarg1 myarg2 -- grep "a b c" foo.txt

In this case argv looks like :

argv = {"my_commandline", "myarg1", "myarg2", "--", "grep", "a b c", "foo.txt"}

Note that the quotes around "a b c" are stripped by the shell so if I simply concatenate the arguments in order to create the arg string for ProcessStartInfo I get:

args = "my_commandline myarg1 myarg2 -- grep a b c foo.txt"

Which is not what I want.

Is there a simple way to either pass an argv to subprocess launch under C# OR to convert an arbitrary argv into a string which is legal for windows and linux shell?

Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about shell