5
Answers

GetCommandLineArgs() method

Photo of Akash Ahlawat

Akash Ahlawat

13y
12.4k
1
Hi guys,
Can anyone tell me what is the use of GetCommandLineArgs() method in C#.

Answers (5)

0
Photo of Senthilkumar
NA 15.2k 2.4m 13y
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:
0
Photo of Vulpes
NA 98.3k 1.5m 13y
Notice that the string array returned by the Environment.GetCommandLineArgs() method differs from the one returned by the args parameter to the Main() method.

The former returns the file name of the executing program as the first element of the array. The latter does not.

GetCommandLineArgs() is used when you want to retrieve the program's command line arguments from somewhere other than the Main() method or to retrieve the program's file name. There are, however, other ways to do the second of these operations.
0
Photo of MuthuMari M
NA 910 11.6k 13y
Hi,

With GetCommandLineArgs() method, the command line arguments can be accessed.
The value returned is an array of strings. It is a method of the System.Environment class. See the code example below:
C# Example
public static int Main(string[] args)
{
 string[] strArgs = System.Environment.GetCommandLineArgs();
 Console.WriteLine("Arguments {0}", strArgs[0]);
}



Thanks.


If this post is useful then mark it as "Accepted Answer"
0
Photo of Kunal Naik
NA 425 0 13y
Hi,

Please refer below link,

Click Here
0
Photo of Jignesh Trivedi
NA 61.3k 14.2m 13y
hi,

The GetCommandLineArgs() method is used to access the command line arguments.
it Returns a string array containing the command-line arguments for the current process.
this method is from System.Environment class

Example
String[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));


hope this help.