Sunday, December 5, 2010

Command Line Parameters C#

Following code shows how command line parameters can accessed in C#.

//CommandLine3.cs
// Commandline arguments: A B C
using System;

namespace CommandLine3
{             
        public class CommandLine3
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("You have entered the following {0} command line arguments:",args.Length);
                for (int i = 0; i < args.Length; i++)
                {
                    Console.WriteLine("{0}", args[i]);
                }
                Console.ReadLine();
            }
        } 
}

Note that:
  • The Length property is used to obtain the length of the array.
  • Length is a read-only property
Following expanation will be a guide on building and running the code within Visual Studio.
  • Right-click the project(in the above example CommandLine3 will be the project) and click Properties this will open up Open the configuration Properties window and click Debugging tab.





  • In the Command Line Arguments property, type "A B C" and save the project





  • Run the project, make sure project has set as the startup project(to do this right click on the project and click on set as startup project

For running the code using command-line use following commands
  • csc CommandLine.cs
  • CommandLine.exe A B C

output


references : http://msdn.microsoft.com/

1 comment :

  1. a simple command line argument program

    http://csharp.net-informations.com/overview/csharp-commandline-arguments.htm

    ling.

    ReplyDelete