How do I read console application parameters?

Category: Introduction, viewed: 16K time(s).

This example display how to pass some parameters as the application arguments, reads it and use it in our console application. The parameters passed to the application as an array of string. When no parameter passed the array length will be zero.

using System;

namespace Kodecsharp.Example.Intro
{
    class CommandLineParameter
    {
        /// <summary>
        /// Reading command line parameter.
        /// </summary>
        /// <param name="args">command line parameteres</param>
        public static void Main(string[] args)
        {
            Console.WriteLine("Argument size: " + args.Length);

            //
            // Print out the entire command line parameters passed 
            // into this program.
            //
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine("Argument[" + i + "] : " + args[i]);
            }
        }
    }
}
Powered by Disqus