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

本文关键字:运行 应用程序 参数 何使用 批处理文件 | 更新日期: 2023-09-27 18:26:33

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

pathname'helloworld'bin'Debug'helloworld.exe 

如果我的程序接收了参数,我该如何调整。的回声何时使用?有关于编写批处理文件的好教程吗?感谢

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

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"answers"is a jerk",你可以在.bat:中写下

helloworld.exe Bob"是个混蛋"

Bob成为第一个参数,因为它周围有空格。但由于有引号,"is a jerk"是一体的。这将是两个参数。

你的标签提到了C,但我不清楚你是否真的是说你用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语句,任何你想输出到控制台窗口的东西。。。