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]
System.Environment.CommandLineincludes the executable and arguments as a single string.http://msdn.microsoft.com/en-us/library/system.environment.commandline.aspx
The
argsparameter is an array of arguments. So while you can parse the individual arguments fromSystem.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 ofMain(), which is probably a bad idea anyway. YourMain()method should handle arguments and pass them around the rest of the application as necessary.