Hi akash,
GetCommandLineArgs method returns a string array containing the command-line arguments for the current process. Although you can receive command line arguments in the Main entry point in the C# language, the GetCommandLineArgs method can be used anywhere.
For example,
using System;
class Program
{
static void Main(string[] args)
{
// Args does not contain the program path.
Console.WriteLine(string.Join(",", args));
// GetCommandLineArgs contains the program path as the first element.
string[] array = Environment.GetCommandLineArgs();
Console.WriteLine(string.Join(",", array));
}
}
The same thing can be done through the array command arquments.
using System;
class Program
{
static void Main(string[] args)
{
if (args == null)
{
Console.WriteLine("args is null"); // Check for null array
}
else
{
Console.Write("args length is ");
Console.WriteLine(args.Length); // Write array length
for (int i = 0; i < args.Length; i++) // Loop through array
{
string argument = args[i];
Console.Write("args index ");
Console.Write(i); // Write index
Console.Write(" is [");
Console.Write(argument); // Write string
Console.WriteLine("]");
}
}
Console.ReadLine();
}
}
For more info: