【问题标题】:How to run a c# application with arguments from a batch file如何使用批处理文件中的参数运行 c# 应用程序
【发布时间】:2013-08-07 13:54:59
【问题描述】:

我有一个 C# 项目,它接受我试图从 bat 文件运行的参数。对于不带参数的应用程序,我只需将以下内容放入名为 run.bat 的文件中

pathname\helloworld\bin\Debug\helloworld.exe 

如果我的程序接受了参数,我该如何调整。什么时候使用回声?关于编写批处理文件的任何好的教程?谢谢

【问题讨论】:

  • c#c 是不同的语言。标记时请多加小心。
  • "pathname\helloworld\bin\Debug\helloworld.exe" "first param" "second param".
  • @IvayloStrandjev 我想这更像是一个蝙蝠标签 :)
  • 你能发布你的 main 方法的代码吗?因为它需要接受参数才能工作,例如 string[] args 是控制台应用程序的默认设置

标签: c# console-application


【解决方案1】:
pathname\helloworld\bin\Debug\helloworld.exe "argument 1" "argument 2" 3

using System;
public class Demo {
    public static void Main(string[] args) {
        foreach(string arg in args)
            Console.WriteLine(arg);
    }
}

【讨论】:

    【解决方案2】:

    我会试试的

    @rem turn off echo - atsign is line-level way how to do it
    @echo off
    @rem provided your app takes three params, this is how to pass them to exe file
    pathname\helloworld\bin\Debug\helloworld.exe %1 %2 %3
    

    【讨论】:

      【解决方案3】:

      字符串参数往往只跟在 EXE 后面的空格后面。因此,如果您有两个参数“Bob”和“is a jerk”,您可以在 .bat 中这样写:

      helloworld.exe Bob“是个混蛋”

      Bob 成为第一个参数,因为它周围有空格。但是“是个混蛋”因为引号而全是一个。所以这将是两个参数。

      您的标签提到了 C,但我不清楚您是否真的是从 C, the completely seperate language 调用它;您似乎只是在表明您使用的是批处理文件。

      【讨论】:

      • 我不知道为什么,但我一直在页面上下滚动寻找一个叫 Bob 的人...
      【解决方案4】:

      对于你的bat文件,只要在你的exe路径后面加上参数,像这样:

      pathname\helloworld\bin\debug\helloworld.exe param1 param2
      

      然后,您的 Program.cs 文件中有一个方法如下所示:

      [STAThread]
      static void Main(string[] args)
      {
        Application.Run(args.Length > 0 ? new Main(args[0]) : new Main());
      }
      

      您可以在此处调整已处理的参数并将它们发送到您的启动表单。

      至于回显,就像一个打印语句,任何你想输出到控制台窗口的东西......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        • 2017-03-28
        • 2023-03-05
        相关资源
        最近更新 更多