繁体   English   中英

如何使用批处理文件中的参数运行c#应用程序

[英]How to run a c# application with arguments from a batch file

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

pathname\helloworld\bin\Debug\helloworld.exe 

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

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);
    }
}

我会尝试

@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

字符串参数倾向于在空格后跟随EXE。 因此,如果您有两个参数“Bob”,并且“是一个混蛋”,您可以在.bat中写这个:

helloworld.exe Bob“是一个混蛋”

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

你的标签提到C,但我不清楚你是否真的意味着你用C语言来称呼它,这是一种完全独立的语言 ; 你似乎只是在指示你使用批处理文件。

对于你的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());
}

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

至于echo,就像print语句一样,你要输出到控制台窗口的任何东西......

暂无
暂无

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

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