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

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


MSDN has a description of how the MS Visual C Runtime parses the string returned by GetCommandLine() into an argv array.

You might also be interested in the list2cmdline() function from the Python standard library that is used by Python's subprocess module to emulate the Unix argv behavior in a Win32 environment.


In windowsland, it's simple really...add extra quotation marks in the string you pass to the System.Diagnostics.ProcessStartInfo object.

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


Thanks to all for the suggestions. I ended up using the algorithm from shquote (http://www.daemon-systems.org/man/shquote.3.html).

/** * Let's assume 'command' contains a collection of strings each of which is an * argument to our subprocess (it does not include arg0). */string args = "";string curArg;foreach (String s in command) {    curArg = s.Replace("'", "'\\''"); // 1.) Replace ' with '\''    curArg = "'"+curArg+"'";          // 2.) Surround with 's    // 3.) Is removal of unnecessary ' pairs. This is non-trivial and unecessary    args += " " + curArg;}

I've only tested this on linux. For windows you can just concatenate the args.