简体   繁体   English

发布版本中的C#命令行参数问题

[英]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: 我有一个WinForm应用程序,其主要方法已修改为可以接受如下命令行参数:

    [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. 在调试模式下使用以下执行行编译时,此代码可以正常工作,并且argCount等于2: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. 但是,当我在发布模式下编译程序时,argCount现在为1,具有相同的命令行参数。 The only argument contains "-file test.txt". 唯一的参数包含“ -file test.txt”。 More than that it only happens if I run the compiled executable from obj/Release folder, but not from bin/Release. 不仅如此,仅当我从obj / Release文件夹而不是bin / Release运行编译的可执行文件时,它才会发生。 Unfortunately setup project takes executables from obj/Release so I can't change that. 不幸的是,安装项目从obj / Release获取可执行文件,因此我无法更改它。 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): 然后从各个位置获得100%一致的结果,将参数“合并”的唯一方法是将其用命令行括在引号中(特别是在那里,您可以将参数包含空格,请参见下面的最后一个示例):

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). 但是,诸如Process Explorer之类的工具将显示用于启动程序的命令行(双击进程并查看image选项卡)。

This works for me from bin/Debug, bin/Release, obj/Debug and obj/Release: 这从bin / Debug,bin / Release,obj / Debug和obj / Release对我有用:

static class Program {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args) {
    FormMain.Args = args;

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new FormMain());
  }
}

  public partial class FormMain: Form {
    public static string[] Args;

    public FormMain() {
      InitializeComponent();
    }

    private void FormMain_Shown(object sender, EventArgs e) {
      foreach (string s in Args) {
        MessageBox.Show(s);
      }
    }
  }

您是否尝试过Environment.GetCommandLineArgs()

Your problem (as Richard points out within his code) is that your arguments when you are running the release version are all enclosed in one set of quotes. 您的问题(正如Richard在其代码中指出的那样)是,在运行发行版时,您的参数都包含在一组引号中。 Remove the quotes and it will work. 删除引号,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM