Why does C # process the args command line inconsistently?

In C #, getting command line arguments directly from Main () excludes the exe name, unlike the C tradition.

Getting the same command line commands through Environment.GetCommandLineArgs enables it.

Is there some good logical reason why I miss this apparent inconsistency?

class Program { static void Main(string[] args) { Console.WriteLine(string.Format("args.Length = {0}", args.Length)); foreach(string arg in args) { Console.WriteLine(string.Format("args = {0}", arg)); } Console.WriteLine(""); string[] Eargs = Environment.GetCommandLineArgs(); Console.WriteLine(string.Format("Eargs.Length = {0}", Eargs.Length)); foreach (string arg in Eargs) { Console.WriteLine(string.Format("Eargs = {0}", arg)); } } } 

Output:

 C:\\ConsoleApplication1\ConsoleApplication1\bin\Debug>consoleapplication1 xx zz aa args.Length = 3 args = xx args = zz args = aa Eargs.Length = 4 Eargs = consoleapplication1 Eargs = xx Eargs = zz Eargs = aa 
+8
command-line c # visual-studio
source share
3 answers

Because it is not C and therefore not attached to them. The need for an exe name is pretty much an edge case; a small number of times when I need it (compared to other arguments). IMO justifies the decision to omit it.

This is additionally required in the specification (ECMA334v4, Β§10.1); (switching to the corresponding parts):

10. Basic concepts

10.1 Application Launch

...

This entry point method is always called Main and must have one of the following signatures:

 static void Main() {…} static void Main(string[] args) {…} static int Main() {…} static int Main(string[] args) {…} 

...

β€’ Let args be the name of the parameter. If the length of the array indicated by the args symbol is greater than zero, the elements of the args[0] array through args[args.Length-1] , inclusive, must refer to strings called application parameters that are set to values ​​determined by the host environment prior to launch applications. The goal is to provide application information determined prior to launching the application from another location in the hosting environment. If the host environment is not capable of supplying strings with uppercase and lowercase letters, the implementation must ensure that strings are received in lowercase. [Note: on systems that support command line parameters, applications, applications correspond to what are usually called command line arguments. end note]

+15
source share

[status-on-design] - http://msdn.microsoft.com/en-us/library/acy3edy3(v=VS.100).aspx

Unlike C and C ++, the program name is not considered the first command line argument.

+8
source share

For me, the reason the two methods return different results is related to Context .

  • The Environment class is used to control the current environement and process, and it makes sense that Environment.GetCommandLineArgs(); returns the executable name as it is part of the process.

  • As for the args array, for me , it makes sense to exclude the executable name. I know that I am invoking an executable file, and in the context of launching my application, I want to know what arguments were sent to it.

At the end of the day, it is very important to be able to get both alternatives.

+5
source share

Source: https://habr.com/ru/post/650135/


All Articles