Get Command Line Arguments

Shows how to get what was typed at the command line. This is a bit different than getting args passed into console app via Main method's string[] args.

Download example.

Listing:

using System;

sealed class ConsoleArgsDemo
{
    public static void Main()
    {
        string[] args = Environment.GetCommandLineArgs();
        Console.WriteLine(" GetCommandLineArgs(): {0}", String.Join(", ", args));
        switch (args.Length)
        {
            case 0:
                //won't ever be reached
                Console.WriteLine(" Nothing entered on command line.");
                break;
            case 1:
                Console.WriteLine(" One word was typed on the command line: {0}", args[0]);
                break;
            case 2:
                Console.WriteLine(" Two words entered on command line: {0}, {1}.", args[0], args[1]);
                break;
        }
    }
}

Author: kyrathasoft

The only programmer in Glens Fork, Ky.