簡體   English   中英

執行包含批處理文件(.bat)中的參數的ac#控制台應用程序

[英]Executing a c# console application that contains parameters from a batch file (.bat)

我正在使用一個控制台應用程序,該應用程序讀取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消息”。

似乎未考慮參數,並且未生成文件

我應該更改什么才能正確執行此操作?

好吧,您的參數在string [] args ,這是一個參數。

嘗試將此行插入代碼並運行它。

Console.WriteLine(args[0]);

它應該顯示您輸入的第一個參數。

因此,在您的代碼更改中

string FileName=Console.ReadLine();

對於

string FileName=args[0];

這應該夠了吧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM