Question: Given a file name and an arbitrary list of strings, is there a canonical way to create a single command line such that Environment.GetCommandLineArgs (and C#'s void main(String[] args)
/ VB's Sub Main(args() As String)
) will return the same list of strings?
Background: The way .NET splits a command line into arguments is surprisingly complex, e.g.:
If a double quotation mark follows two or an even number of backslashes, each proceeding backslash pair is replaced with one backslash and the double quotation mark is removed. If a double quotation mark follows an odd number of backslashes, including just one, each preceding pair is replaced with one backslash and the remaining backslash is removed; however, in this case the double quotation mark is not removed.
Many try the simple "put every argument in double quotes and escape existing double quotes" approach and fail as soon as one of the arguments contains a trailing backslash. There have been various questions on StackOverflow regarding this issue, e.g.:
- How to escape path containing spaces
- Escape command line arguments in c#
- Escape string for Process.Start
- Passing command-line arguments in C#
However, their answers are either not general enough to provide a canonical solution for all cases or appear to be developed "iteratively" ("Oh, there's one more special case I forgot, let's add it and now it should cover most cases..."). Since this is quite a common problem, I'd like to see a solution that provides confidence, for example, by either
- coming from an authoritative source (maybe a blog entry from one of the developers involved in this crazy command line convention) or
- providing a formal proof that the given algorithm satisfies the .NET command line requirements.