1

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 ?

Amibluesky
  • 179
  • 1
  • 13
  • 1
    Set a breakpoint at the top of the `Main` function and look at the parameter `args`. Also, this answer will help you https://stackoverflow.com/a/298713/9365244 – JayV Jun 17 '19 at 13:07
  • 4
    You cannot get parameters from your batch using `Console.ReadLine()`. Use the argument `args` of the `Main()` method instead. – Erik T. Jun 17 '19 at 13:09
  • 1
    reading data from Console.ReadLine is not the same as getting parameters. Only way you can do that would be to send data to it, either through pipe or by < somefile.txt etc. – BugFinder Jun 17 '19 at 13:10
  • Yes, look at the link from JayV so you can set the command line parameters and debug from within visual studio. – Derek Jun 17 '19 at 13:15

1 Answers1

2

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.

MartinS
  • 751
  • 3
  • 12
  • 27