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

I inherit the service on several console applications, which are introduced naturally with static void Main(string[] args). However, the code ignores the array argsand instead reads the command line options from System.Environment.CommandLine.

Is there a functional difference?

The content looks identical. In any case, I would suspect a momentary performance hit by triggering System.Environment.CommandLine(but not so much that I was ever worried or careful enough to measure).


UPDATE: I suspected it System.Environment.CommandLineshould contain an executable path, but I did not see it ... because I was looking for the wrong place. The ALSO code has string[] arrCmdLine = System.Environment.GetCommandLineArgs();.... System.Environment.CommandLine.ToLower()checked for the presence of "debug", while all other parameters are extracted from GetCommandLineArgs(), and I mentally merged the two while I walked "why not just use it args[]?"

For many years, I was tormented by a better way to parse command line arguments, when it all was "putting them in the correct order!". [Jk]

+7
source share
1 answer

System.Environment.CommandLine includes the executable and arguments as a single line.

// 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

args . , System.Environment.CommandLine, , . , , - Main(), , , . Main() .

+10

All Articles