Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

.net - C# Command Line arguments problem in Release build

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The command line processing should be the same, therefore something else is going on. When I try this:

class Program {
    [STAThread]
    static void Main(String[] args) {
        Console.WriteLine("Have {0} arguments", args.Length);
        for (int i = 0; i < args.Length; ++i) {
            Console.WriteLine("{0}: {1}", i, args[i]);
        }
    }
}

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

PS C:...inDebug> .ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:...inDebug> pushd ..
elease
PS C:...inRelease> .ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:...inRelease> pushd ....objdebug
PS C:...objDebug> .ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:...objDebug> pushd ..
elease
PS C:...objRelease> .ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:...objRelease> .ConsoleApplication1.exe -file test.txt
Have 2 arguments
0: -file
1: test.txt
PS C:...objRelease> .ConsoleApplication1.exe "-file test.txt"
Have 1 arguments
0: -file test.txt

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...