How to make a batch file run a Console Application by passing parameter?

Asked

Viewed 1,205 times

3

I have a Console Application that method main of Program.Cs (where the application starts) receives a input.

Follows 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 file batch to execute this application but send the expected parameter in the variable input.

1 answer

3


You can use the command start to run the file.

@echo off
start /b consoleApp.exe foo

And to receive 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.