I have what seems to be a simple problem that I can’t solve myself. I have a WinForm app, with main method modified to accept command line arguments like this:
[STAThread] static void Main(String[] args) { int argCount = args.Length; }
This code works fine and argCount is equals to 2 when compiled in debug mode with the following execution line: program.exe -file test.txt. However as soon as I compile the program in release mode, argCount is now 1 with the same command line arguments. The only argument contains ‘-file test.txt’. More than that it only happens if I run the compiled executable from obj/Release folder, but not from bin/Release. Unfortunately setup project takes executables from obj/Release so I can’t change that. Is this a known issue and is there a way around this problem?
The command line processing should be the same, therefore something else is going on. When I try this:
and then it from the various locations I get 100% consistent results, the only way of getting arguments ‘merged’ is to enclose them in quotes on the command line (which is specifically there to allow you do have arguments containing a space, see the last example below):
Additional While launching from a command prompt makes it easy to see what is being passed it can be hard to check when another application launches yours. However tools like Process Explorer will show the command line used to start a program (double click on a process and look at the image tab).