发布版本中的 C# 命令行参数问题 [英] C# Command Line arguments problem in Release build

查看:23
本文介绍了发布版本中的 C# 命令行参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个我自己无法解决的看似简单的问题.我有一个 WinForm 应用程序,主要方法修改为接受这样的命令行参数:

 [STAThread]static void Main(String[] args){int argCount = args.Length;}

这段代码工作正常,在调试模式下编译时,argCount 等于 2,执行行如下:program.exe -file test.txt.但是,只要我在发布模式下编译程序,argCount 现在就为 1,具有相同的命令行参数.唯一的参数包含-file test.txt".更重要的是,只有当我从 obj/Release 文件夹而不是从 bin/Release 运行编译的可执行文件时才会发生这种情况.不幸的是,安装项目从 obj/Release 获取可执行文件,所以我无法改变它.这是一个已知问题吗?有没有办法解决这个问题?

解决方案

命令行处理应该是一样的,因此其他事情正在发生.当我尝试这个时:

class 程序 {[STAThread]静态无效主(字符串 [] args){Console.WriteLine("有 {0} 个参数", args.Length);for (int i = 0; i < args.Length; ++i) {Console.WriteLine("{0}: {1}", i, args[i]);}}}

然后它从各个位置我得到 100% 一致的结果,获取参数合并"的唯一方法是将它们括在命令行上的引号中(专门用于允许您确实有包含空格的参数,请参阅下面的最后一个示例):

<前>PS C:\...\bin\Debug> .\ConsoleApplication1.exe 一二三有 3 个参数0:一1:两个2:三个PS C:\...\bin\Debug> pushd ..\releasePS C:\...\bin\Release> .\ConsoleApplication1.exe 一二三有 3 个参数0:一1:两个2:三个PS C:\...\bin\Release> pushd ..\..\obj\debugPS C:\...\obj\Debug> .\ConsoleApplication1.exe 一二三有 3 个参数0:一1:两个2:三个PS C:\...\obj\Debug> pushd ..\releasePS C:\...\obj\Release> .\ConsoleApplication1.exe 一二三有 3 个参数0:一1:两个2:三个PS C:\...\obj\Release> .\ConsoleApplication1.exe -file test.txt有 2 个参数0:-文件1:测试.txtPS C:\...\obj\Release> .\ConsoleApplication1.exe "-file test.txt"有 1 个参数0: -file test.txt

附加 虽然从命令提示符启动可以轻松查看传递的内容,但很难检查其他应用程序何时启动您的应用程序.但是,像 Process Explorer 之类的工具将显示命令用于启动程序的行(双击进程并查看图像选项卡).

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:

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:\...\bin\Debug> .\ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:\...\bin\Debug> pushd ..\release
PS C:\...\bin\Release> .\ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:\...\bin\Release> pushd ..\..\obj\debug
PS C:\...\obj\Debug> .\ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:\...\obj\Debug> pushd ..\release
PS C:\...\obj\Release> .\ConsoleApplication1.exe one two three
Have 3 arguments
0: one
1: two
2: three
PS C:\...\obj\Release> .\ConsoleApplication1.exe -file test.txt
Have 2 arguments
0: -file
1: test.txt
PS C:\...\obj\Release> .\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).

这篇关于发布版本中的 C# 命令行参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆