执行包含批处理文件(.bat)中的参数的c#控制台应用程序

时间:2019-06-17 13:05:01

标签: c# batch-file

我正在使用一个控制台应用程序,该应用程序读取xml文件,进行一些计算,然后提供其他xml文件

我向您展示了一部分代码:

static void Main(string[] args)
    {
        Console.WriteLine("Please provide the file name of your xml input (without the \".xml\" extention): ");
        string FileName = Console.ReadLine();

        string entry = null;
        if (FileName == "NetworkGTCS_7" || FileName == "NetworkGTCS_6")
        {
            Console.WriteLine("Please Provide the level's number to reach \"Level should be between 1 to 7\"");
            Console.WriteLine("N-B: if the provided level is greater than \"5\", the operation will take few minutes to generate the output file");
            entry = Console.ReadLine();
        }
        else if  .... etc
        .
        .
        .

        Console.WriteLine($"The output file \"NXRoutesFor({FileName})level{level}.xml\" is generated with success !");
        Console.WriteLine("Tap \"ENTER\" key to exit...");
        Console.ReadLine();

        ...etc
    }

因此,如代码中所示,我的应用程序收到两个参数:第一个是文件名(字符串),第二个是数字(整数)

我创建了以下批处理文件以启动我的应用程序:

@echo off
C:\NXRoutesCalculationApp\NXRoutesCalculation.exe NetworkGTCS_2 7

当我单击此文件时,控制台将打开并显示“ WriteLine消息”。

似乎没有考虑参数,并且没有生成文件

要正确执行此操作,我应该更改什么?

1 个答案:

答案 0 :(得分:2)

好吧,您的参数位于string [] args中,这是一个参数。

尝试将此行插入代码并运行。

Console.WriteLine(args[0]);

它应该显示您输入的第一个参数。

因此,在您的代码更改中

string FileName=Console.ReadLine();

string FileName=args[0];

应该可以解决问题。

相关问题