Sunday, December 5, 2010

Command Line Parameters C#

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

// 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]);

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


references :

1 comment :

  1. a simple command line argument program