I'm inheriting maintenance on several console application that are entered, naturally, with static void Main(string[] args)
. However, the code ignores the args
array, and instead reads the command-line parameters from System.Environment.CommandLine
.
Is there a functional difference, here?
The contents look identical. If anything, I would suspect a minute performance-hit by invoking System.Environment.CommandLine
(but not enough that I would ever be concerned or care enough to measure).
UPDATE: I suspected that System.Environment.CommandLine
should contain the executable path, but I wasn't seeing it... because I was looking in the wrong place. The code ALSO has string[] arrCmdLine = System.Environment.GetCommandLineArgs();
.... System.Environment.CommandLine.ToLower()
is checked for the presence of "debug" while all other params are extracted from GetCommandLineArgs()
and I was mentally conflating the two while I was going "why not just use args[]
?"
For years I've agonized over the best way of parsing command-line args, when all along it was "place them in the correct order!" [jk]
The string[] args is a variable that has all the values passed from the command line as shown above. Now to print those arguments, let's say we have an argument, “One” − Console. WriteLine("Length of the arguments: "+args. Length); Console. WriteLine("Arguments:"); foreach (Object obj in args) { Console.
What is the use of GetCommandLineArgs() method ? GetCommandLineArgs() returns a string array containing the command-line arguments for the current process. The first element in the array contains the file name of the executing program. The remaining elements contain any additional tokens entered on the command line.
It is the first method which gets invoked whenever an application started and it is present in every C# executable file. The application may be Console Application or Windows Application. The most common entry point of a C# program is static void Main() or static void Main(String []args) .
System.Environment.CommandLine
includes the executable and arguments as a single string.
// Sample for the Environment.CommandLine property.
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
// Invoke this sample with an arbitrary set of command line arguments.
Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
}
}
/*
This example produces the following results:
C:\>env0 ARBITRARY TEXT
CommandLine: env0 ARBITRARY TEXT
*/
http://msdn.microsoft.com/en-us/library/system.environment.commandline.aspx
The args
parameter is an array of arguments. So while you can parse the individual arguments from System.Environment.CommandLine
, I'm not sure why you would want to. The only reason I can see is if you need to access arguments outside of Main()
, which is probably a bad idea anyway. Your Main()
method should handle arguments and pass them around the rest of the application as necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With