Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanly passing a list of arguments to ProcessStartInfo

Arguments are received as string[] in a fresh program. They are already parsed correctly, so quotes will keep a parameter together, even though it may be a path that contains spaces. Nice.

I now want to forward them to a new Process, but ProcessStartInfo only supports string to define arguments.

So, just concatenating them with spaces is wrong, as paths may then be split into multiple arguments. I'm also unsure if wrapping them in quotes and then concatening them with spaces works in every instance - I don't think so, as the target program may not see -u and "-u" as equivalent. Is there a clean way to do this in .NET Framework?

like image 603
AyCe Avatar asked Nov 08 '25 01:11

AyCe


1 Answers

ProcessStartInfo also has an ArgumentList property which allows for passing multiple arguments.

ArgumentList and the Arguments property are independent of one another and only one of them can be used at the same time. The main difference between both APIs is that ArgumentList takes care of escaping the provided arguments and internally builds a single string that is passed to operating system when calling Process.Start(info). So if you are not sure how to properly escape your arguments, you should choose ArgumentList over Arguments.

Reference


EDIT

Applies to

Product Versions
.NET .NET 5, .NET 6, .NET Core 2.1, .NET Core 2.2, .NET Core 3.0, .NET Core 3.1
.NET Standard 2.1
like image 189
Sarin Avatar answered Nov 09 '25 18:11

Sarin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!