简体   繁体   中英

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

I'm working on a console App that reads xml files, does some calculation and then provides other xml files

I show you a part of code:

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
    }

So as shown in the code my app receives two parameters: the first one is a file name (string) and the second one is Number (integer)

I created the following Batch file in order to launch my app :

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

when i click on this file the console opens and shows the "WriteLine Message" .

it seems that the parameters are not taken in consideration, and no file is generated

What should I change to perform this correctly ?

Well, your params are in string [] args which is a paramerter.

Try insert this line into your code and run it.

Console.WriteLine(args[0]);

It should show you the first parameter you've entered.

So in your code change

string FileName=Console.ReadLine();

for

string FileName=args[0];

That should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM