【问题标题】:Executing a c# console application that contains parameters from a batch file (.bat)执行包含来自批处理文件 (.bat) 的参数的 c# 控制台应用程序
【发布时间】:2019-06-17 13:05:01
【问题描述】:

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

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

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

【问题讨论】:

  • Main函数顶部设置断点,查看参数args。另外,这个答案会帮助你stackoverflow.com/a/298713/9365244
  • 您无法使用 Console.ReadLine() 从批处理中获取参数。请改用Main() 方法的参数args
  • 从 Console.ReadLine 中读取数据与获取参数不同。您可以这样做的唯一方法是通过管道或 向其发送数据
  • 是的,请查看 JayV 的链接,以便您可以在 Visual Studio 中设置命令行参数和调试。

标签: c# batch-file


【解决方案1】:

嗯,你的参数在 string [] args 这是一个参数。

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

Console.WriteLine(args[0]);

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

所以在你的代码改变

string FileName=Console.ReadLine();

string FileName=args[0];

这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2015-06-23
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多