[Mono-dev] sub-process invocation on posix

Ian Norton inorton at gmail.com
Mon Jun 10 19:46:03 UTC 2013


I kind of already have a thing to do that, feels a bit icky though,
especially as there must be some thing lower down that undoes the joined up
string into a char** again. :)


On 10 June 2013 16:06, Michael Hutchinson <m.j.hutchinson at gmail.com> wrote:

> 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/05be5d3f/attachment.html>


More information about the Mono-devel-list mailing list