How do I make a batch file run an Application Console passing parameter?

3

I have an Application Console that the% method of Program.cs (where it starts the application) receives an input .

Follow the code below:

static void Main(string[] args)
{   
    var input = Console.ReadLine();

    ControleEstado.IniciaControle(input);

    Console.WriteLine();
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

I need to create a batch file that runs this application, but send the expected parameter to the input variable.     

asked by anonymous 28.04.2015 / 18:26

1 answer

3

You can use the start command to run the file.

@echo off
start /b consoleApp.exe foo

And to get the arguments passed the application do:

static void Main(string[] args)
{
    foreach (string arg in args) {
        string input = arg.Trim();
        switch (input) {
            case "foo":
                Console.WriteLine("Foo");
                break;
            case "bar":
                Console.WriteLine("Bar");
                break;
            case "baz":
                Console.WriteLine("Baz");
                break;
            default:
                Console.WriteLine(input);
                break;
            }
        }
    Console.WriteLine();
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}
    
28.04.2015 / 18:38