Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发布版本中的C#命令行参数问题_C#_.net_Winforms - Fatal编程技术网

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

发布版本中的C#命令行参数问题,c#,.net,winforms,C#,.net,Winforms,我有一个似乎很简单的问题,我自己解决不了。我有一个WinForm应用程序,修改了main方法以接受如下命令行参数: [STAThread] static void Main(String[] args) { int argCount = args.Length; } 当在调试模式下使用以下执行行编译时,此代码工作正常,argCount等于2:program.exe-file test.txt。 但是,一旦我在发布模式下编译程序,argCount现

我有一个似乎很简单的问题,我自己解决不了。我有一个WinForm应用程序,修改了main方法以接受如下命令行参数:

    [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 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]);
        }
    }
}
类程序{
[状态线程]
静态void Main(字符串[]参数){
WriteLine(“具有{0}个参数”,args.Length);
对于(int i=0;i
然后从不同的位置我得到100%一致的结果,得到参数“合并”的唯一方法是在命令行中将它们括在引号中(该命令行特别允许您的参数包含空格,请参见下面的最后一个示例):

PS C:\…\bin\Debug>\ConsoleApplication1.exe一二三 有三个论点 0:1 1:2 2:3 PS C:\…\bin\Debug>pushd..\release PS C:\…\bin\Release>\ConsoleApplication1.exe一二三 有三个论点 0:1 1:2 2:3 PS C:\..\bin\Release>pushd..\obj\debug PS C:\…\obj\Debug>\ConsoleApplication1.exe一二三 有三个论点 0:1 1:2 2:3 PS C:\…\obj\Debug>pushd.\release PS C:\…\obj\Release>\ConsoleApplication1.exe一二三 有三个论点 0:1 1:2 2:3 PS C:\…\obj\Release>\ConsoleApplication1.exe-file test.txt 有两个论点 0:-文件 1:test.txt PS C:\…\obj\Release>\ConsoleApplication1.exe“-file test.txt” 有1个参数 0:-文件test.txt
另外,当从命令提示符启动时,可以很容易地看到正在传递的内容,而当另一个应用程序启动您的应用程序时,很难进行检查。但是,像这样的工具将显示用于启动程序的命令行(双击进程并查看图像选项卡)。

这对我来说适用于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);
      }
    }
  }
静态类程序{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main(字符串[]参数){
FormMain.Args=Args;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(newformmain());
}
}
公共部分类FormMain:Form{
公共静态字符串[]Args;
public FormMain(){
初始化组件();
}
显示私有void FormMain_(对象发送方,事件参数e){
foreach(Args中的字符串s){
MessageBox.Show(s);
}
}
}

您的问题(正如Richard在其代码中指出的)是,在运行发布版本时,您的参数都包含在一组引号中。删除引号,它就会工作。

谢谢!不过,我将尝试这样做,因为第一个参数是程序的名称,所以需要对代码进行重大更改。似乎应该有办法解决发布版本中存在的问题。尝试过了,仍然不起作用:-(来自自定义日志文件的输出:First Key=[-文件C:\Temp\test.dpk]参数数:1个环境参数数:2个参数数就是参数。长度,环境参数数就是环境。GetCommandLineArgs()。LengthMy不好,我是个白痴:-)我刚刚注意到我的安装程序的文件格式的open命令是“-file{0}”,我所要做的就是删除引号。。。很容易做,也很难发现。。。将添加一个注释,说明如何实际检查如果不直接执行所传递的内容。是否有充分的理由使用静态字符串而不是更改ctor的签名以获取参数?不,没有。为了简洁起见,我使用了静态字符串。如您所见,我将Args声明为公共字段。在真实代码中,我永远不会那样做。在这种情况下,我们存储参数的位置并不重要,我们正在解决另一类问题。
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);
      }
    }
  }