Even with modern UI, we often need a way to start our programs with specific
parameters. Command line arguments are helpful to provide those parameters
without exposing them to everybody. When developing with .NET and C# you can get
the command line arguments from your Main(string[] Args) function. Args is in
fact an array containing all the strings separated by spaces entered in the
command line.
Suppose we have an application which accepts some command line parameters to do
some operations. We know how to pass the parameters from command prompt. I havea sample here.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication2010
{
class
CommandLineArgument
{
static void
Main(string[] arguments)
{
foreach (String
arg in
Environment.GetCommandLineArgs())
{
Console.Write(arg);
}
Console.ReadKey();
}
}
}
Now I am going to introduce passing the command line arguments from Visual
Studio. Some times we don't like to pass the arguments from the Command Prompt.
Here is a Quick way to pass the arguments.
Visual Studio enables a nice feature where you can do this in the Project
Properties window, on the Debug tab. Here is the steps to achieve this
-
Right Click on Project from Solution Explorer
and Select Properties.
-
In the Project Properties Windows, Navigate to
"Debug Tab"
-
You will Find the a text box "Command Line".
Please separate the arguments with the comma(',')
or Space also.
Here the Screen Shot to pass the arguments
Here the source code follows :
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication2010
{
class
CommandLineArgument
{
static void
Main(string[] arguments)
{
foreach (String
arg in arguments)
{
Console.Write(arg);
}
Console.Write("Waiting
for User response...");
Console.ReadKey();
}
}
}
Here I put the break point to give the clear view of the arguments passed from
the Debug Tab.
The final output is shown here
I thought this article would be helpful to all. Any suggestions are welcome.
Happy Coding :-)