[Mono-dev] sub-process invocation on posix

Michael Hutchinson m.j.hutchinson at gmail.com
Mon Jun 10 15:06:23 UTC 2013


FWIW, you actually just need to double quote each argument and escape
double quotes so you can very easily write a helper to do this in a way
that works on both Mono and .NET:

static Process StartProcess (string name, params string[] args)
{
    string a = null;
    if (args != null && args.Length > 0)
        a = "\"" + string.Join (args.Select (s => s.Replace ("\"",
"\\\"")).ToArray (), "\" \"") + "\"";
    return Process.Start (
        new ProcessStartInfo (name, a) {
            ShellExecute = false,
        }
    );
}

Obviously this could be done more efficiently with a StringBuilder.

Apologies for any errors, I'm writing this on my phone...

- Michael
On Jun 6, 2013 1:18 PM, "Ian Norton" <inorton at gmail.com> wrote:

> Hiya, I'm aware that I can use Process.Start() but I'd really really like
> to be able to directly pass a list of strings to my child process as
> arguments rather than having to escape shell characters and spaces etc.
>
> Ie, In perl or C I'd do:
>
> system("df","-m","/home/foo/Documents/Pictures/My Holiday");
>
> Where in c# I'm forced to escape the space -> "My\ Holiday"
>
> Ian
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130610/109f30c2/attachment.html>


More information about the Mono-devel-list mailing list