Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Display Command Line Arguments (C# Programming Guide)

Arguments provided to an executable on the command-line are accessible through an optional parameter to Main. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable:

Input on Command-line Array of strings passed to Main

executable.exe a b c

"a"

"b"

"c"

executable.exe one two

"one"

"two"

executable.exe “one two” three

"one two"

"three"

Example

This example displays the command line arguments passed to a command-line application. The output shown is for the first entry in the table above.

class CommandLine
{
    static void Main(string[] args)
    {
        // The Length property provides the number of array elements
        System.Console.WriteLine("parameter count = {0}", args.Length);

        for (int i = 0; i < args.Length; i++)
        {
            System.Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
        }
    }
}

Output

 
parameter count = 3
Arg[0] = [a]
Arg[1] = [b]
Arg[2] = [c]

See Also

Community Additions

ADD
Show:
© 2015 Microsoft