Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between (string[] args) and System.Environment.CommandLine?

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]

like image 422
Michael Paulukonis Avatar asked Jun 14 '12 19:06

Michael Paulukonis


People also ask

What is string [] args in C#?

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 GET command line args method?

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.

Why do we use string args in main method in C#?

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) .


1 Answers

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.

like image 110
jrummell Avatar answered Sep 23 '22 22:09

jrummell