1.5 Access Command-Line Arguments


Problem

You need to access the arguments that were specified on the command line when your application was executed.

Solution

Use a signature for your Main method that exposes the command-line arguments as a string array. Alternatively, access the command-line arguments from anywhere in your code using the static members of the System.Environment class.

Discussion

Declaring your application's Main method with one of the following signatures provides access to the command-line arguments as a string array.

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

At run time, the args argument will contain a string for each value entered on the command line after your application's name . To demonstrate this, the Main method in the following example steps through each of the command- line arguments passed to it and displays them to the console.

 public class CmdLineArgExample {     public static void Main(string[] args) {              // Step through the command-line arguments         foreach (string s in args) {                     System.Console.WriteLine(s);         }     } } 

It's important to understand how the arguments are passed to the application. If you execute the CmdLineArgExample using the following command:

 CmdLineArgExample "one \"two\"    three" four 'five    six' 

the application will generate the following output on the console:

 one "two"    three four 'five six' 

Notice that unlike C and C++, the application's name is not included in the array of arguments. Also notice that the use of double quotes (") results in more than one word being treated as a single argument, although single quotes (') do not. You can include double quotes in an argument by escaping them with the backslash character (\). Finally, notice that all spaces are stripped from the command line unless they are enclosed in double quotes.

If you need access to the command-line arguments at places in your code other than the Main method, you can process the command-line arguments in your Main method and store them for later access. Alternatively, you can use the System.Environment class, which provides two static members that return information about the command line: CommandLine and GetCommandLineArgs . The CommandLine property returns a string containing the full command line that launched the current process. Depending on the operating system on which the application is running, path information might precede the application name. Microsoft Windows NT 4.0, Windows 2000, and Windows XP don't include path information, whereas Windows 98 and Windows ME do. The GetCommandLineArgs method returns a string array containing the command-line arguments. This array can be processed in the same way as the string array passed to the Main method, as discussed at the start of this section. Unlike the array passed to the Main method, the first element in the array returned by the GetCommandLineArgs method is the name of the application.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net