首页 > 解决方案 > 执行包含批处理文件 (.bat) 中的参数的 ac# 控制台应用程序

问题描述

我正在开发一个控制台应用程序,它读取 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 消息”。

好像没有考虑参数,没有生成文件

我应该改变什么才能正确执行此操作?

标签: c#batch-file

解决方案


好吧,您的参数在string [] args哪个参数中。

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

Console.WriteLine(args[0]);

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

所以在你的代码更改中

string FileName=Console.ReadLine();

为了

string FileName=args[0];

这应该够了吧。


推荐阅读