To pass a path with spaces to .NET console application you should escape it. Probably not escape but surround with double quotes:
myapp.exe --path C:\Program Files\MyApp`
becomes
new string[] { "--path", "C:\Program", "Files\MyApp" }
but
myapp.exe --path "C:\Program Files\MyApp"
becomes
new string[] { "--path", "C:\Program Files\MyApp" }
and it works fine and you can parse that easily.
I want to extend the set of parameters given with an addition one and start a new process with the resulting set of parameters:
new ProcessStartInfo(
Assembly.GetEntryAssembly().Location,
String.Join(" ", Enumerable.Concat(args, new[] { "--flag" })))
This becomes myapp.exe --path C:\Program Files\MyApp --flag
where path drops its escaping.
How to workaround it with common solution? (without searching each parameter's value requiring escaping and quoting it manually)
I don't think it is possible since the space is the delimiter for CLI arguments so they would need to be escaped.
You could extract this into an extension method quite nicely so you can just run
args.Escape()
in your code above.Just quote every parameter. This...
...is a perfectly valid command line and does exactly what you want.