【20100829-01】Using args in Main

http://dotnetperls.com/process-start-net

http://dotnetperls.com/main

sing args in Main
First, when you create a new console application in the C# language using Visual Studio, you will get a Main method with a signature similar to the one shown here, with a string[] args parameter and void return type. You can use a Main method that returns an int or that receives no parameters if you want to. This program shows how the command-line parameters are received from a Windows command line and how you can access and test them in the C# program.

~~~ Program that receives command-line arguments (C#) ~~~

 

代码
1 using System;
2
3  class Program
4 {
5 static void Main(string[] args)
6 {
7 if (args == null)
8 {
9 Console.WriteLine("args is null"); // Check for null array
10   }
11 else
12 {
13 Console.Write("args length is ");
14 Console.WriteLine(args.Length); // Write array length
15   for (int i = 0; i < args.Length; i++) // Loop through array
16   {
17 string argument = args[i];
18 Console.Write("args index ");
19 Console.Write(i); // Write index
20   Console.Write(" is [");
21 Console.Write(argument); // Write string
22 Console.WriteLine("]");
23 }
24 }
25 Console.ReadLine();
26 }
27 }
28

 

"C:\ConsoleApplication1.exe" a b c
args length is 3
args index 0 is [a]
args index 1 is [b]
args index 2 is [c]

"C:\ConsoleApplication1.exe" a                 b           c
args length is 3
args index 0 is [a]
args index 1 is [b]
args index 2 is [c]

"C:\\ConsoleApplication1.exe" http://dotnetperls.com/
args length is 1
args index 0 is [http://dotnetperls.com/]

"C:\ConsoleApplication1.exe" "Literal      test "
args length is 1
args index 0 is [Literal      test ]Overview of program text. This program is defined in the Program user-defined class, which isn't an important detail. You can define your Main method in any class name you want that is valid in the C# programming language. Note that you must have a class enclosing all methods in your C# methods, however. Upon startup, the Main method is executed and then tests for a null array argument (this is never reached) and then prints the array length and then writes the arguments as strings. The "Output" section of the example shows possible command lines and the program's actual output.

Whitespace in args
Here we note how the command-line arguments are handled with whitespace in the args array. As the second command-line test in the example shows, more than 1 whitespace is discarded when separating the arguments. So if you separate parameters by two spaces, there is no difference from separating with 1 space. However, the string literal syntax with the " symbols are parsed differently and enclosed whitespace is retained. If you want to preserve whitespace on the command line, use the quotation marks around your parameter.

Null array parameter. In the author's testing, you will not encounter a null parameter array in the Main entry point. So if you execute a program with no command-line parameters, you will not receive a null string[] array reference in the Main method. However, if incorrect parameters are likely, it is best to wrap the Main entry point with try/catch/finally blocks to ensure the best possible execution paths are taken. If a step is critical, put it in a finally block, so even if an exception is raised, the step will be executed. This article is based on the .NET Framework 3.5 SP1.

Method signatures
Here we mention that you can change the signature of the Main method in your program and the program will still compile and execute correctly. You can demand that the Main method return an integer type, which provides a clue to the operating system about the program's result. It is also very useful to omit the string[] args parameter array entirely. This site almost always omits the parameter array to shorten the example programs and improve clarity.

Creating shortcuts
The vast majority of developers using the C# programming language are using the Windows operating system targeting the .NET Framework. If you are using Windows, you can open your program's Release or Debug directory and create a shortcut to your application and specify command-line parameters in that shortcut. Right-click on the EXE file and select Create Shortcut. Then, in the "Target" text box in the Shortcut Properties window, append the command-line arguments after the file name. When you click on the shortcut, these strings will be put into the string[] args array at runtime in the Main method.

Summary
Here we looked at the exact behavior of the Main entry point in the C# programming language when used with an args string[] array. We saw examples of how you can call the Main method from the Windows operating system with shortcuts; how whitespace in the command-line is handled before the arguments are placed into the args string array; and how null arrays are not encountered. Using the args parameter on the Main method provides a convenient way to develop programs that accept input automatically upon invocation.

See Args Loop (Foreach and For).

See Console Overview.

Do not copy this page.

Sam Allen

 

 

posted @ 2010-08-29 21:49  WillWayer  阅读(382)  评论(0编辑  收藏  举报