Canonical solution for escaping .NET command line arguments

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.:

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.

Canonical way to parse the command line into arguments in plain C Windows API

In a Windows program, what is the canonical way to parse the command line obtained from GetCommandLine into multiple arguments, similar to the argv array in Unix? It seems that CommandLineToArgvW does

Eclipse canonical list of command line options

I have googled all the obvious strings, searched through FAQs but cannot find a canonical list of command line options for eclipse. I have installed eclipse on an NFS mounted directory and upon upgrad

Safely escaping arguments on the command line in C#

I’d like to pass some user supplied arguments to an application (using C# on Windows). The arguments are in a NameValueCollection and I wish to pass them as a string so that the application can be inv

subprocess.Popen not escaping command line arguments properly?

I am trying to call the following curl command with python: curl -k -F [email protected] -F data={//title//://Another App//} -Lu usr:pwd For it to work,

Eclipse command line arguments

I understand how to run my application with command line arguments using the run configuration menu. The problem I have is that no matter what I update these command line arguments to, eclipse does no

Modify Command Line Arguments

I have a Win32 C++ application and I need to modify the command line arguments in the application. Specifically, I want to edit the command line arguments in such a way that GetCommandLineW() returns

Java Web Start JAVAWS command line arguments

I need to activate webstart by passing command line arguments in my code. I have seen Passing command line arguments to javaws (Java WebStart) executable. but could not get the solution which can take

Building a ASP.NET solution from command-line?

How can I build an ASP.NET web application from the command line?

QtCreator and Command Line Arguments

Is there a way to send command line arguments to a program being debugged in QtCreator?

Maven Command Line arguments

it is possible to pass arguments from command line to properties in pom file ? for example I run mvn … argument and in pom.xml <properties> <myproperty> here should add argument from com


A few years ago, Microsoft announced that they were going to release a command line parser on CodePlex (instead of the System.Shell.CommandLine that was supposed to ship with .NET Framework 4). I’m not sure if they actually did this. If you want a parser developed by a Microsoft employee have a look at cmdline. Also you can look for command line parses in CodePlex.

You could also try Mono.Options which is quite powerful.

This algorithm is generic and comes from a relatively authoritative source (MSDN blogs).