Command line Arguments



Command line Arguments:
  • It is used to pass one or more input values from the console to the application, before starting the execution of the application.
Syntax:-       App1.exe value1  value2
                       where Command line Arguments are value 1 and value 2
                        Main() method receives the command line arguments, as a string arrey.
                        Static void Main(string[]args)
                       {                                             
                                //manipulate args
                        }
Ex:-
Develop a c#.net console app, which receives 2 values from command line (console) and also displays bigger number of them?
Steps:-

  • Create a new c# console app.

            Name: app1
            Location: C:\

  • Program:
                 using System;

                 namespace app1
                 {
                     class Program
                     {
                         static void Main(string[] args)
                         {
                             if (args.Length == 2)
                             {
                                 int a = Convert.ToInt32(args[0]);
                                 int b = Convert.ToInt32(args[1]);
                                 int big;
                                 if (a > b)

                                     big = a;
                                 else
                                     big = b;
                                 Console.WriteLine("big no is" + big);
                             }
                             else
                                 Console.WriteLine("please provide 2 numbers");
                             Console.ReadKey();
                         }
                     }
                 }

  • “Build” menu -> “Build solution”
  • In the C:\app1\app1\bin\debug folder of the project, system automatically generates a .exe file.
Ex:- C:\app1\app1\bin\debug\app1.exe
  • Copy the app1.exe file from “C:\app1\app1\bin\debug” folder into desktop.
  • Start -->run
  • Cmd
  • Ok
    For windows7 
  • Cd C:\users\administrator\desktop
    App1 50 87    
         For windows XP                             
  • Cd C:\documents and settings\admin\desktop
    App1 50 87
Output:-
            87

No comments:

Post a Comment