How to pass parameters to a program December 21, 2009 at 5:45 pm

The following code prints out the first and second arguments passed to a console application.

    class Program
    {
        static void Main(string[] args)//string[] args is the parameter used to pass arguments to a program
        {
            Console.WriteLine(args[0]);//first argument
            Console.WriteLine(args[1]);//second argument
        }
    }

Getting the arguments to the program can be done in several ways:

In Visual Studio: Under the Project > Double click “Properties” > Debug tab > Command Line Arguments box. Seperate each argument with a space. If the argument contains spaces enclose it with quotes.

Command Line Arguments

From the command line: “MyProgram.exe “hello world” goodbye” (without the outside quotes)

Command Line Args 2

Leave a Reply