Read Command Line Arguments

If you are calling your C# program from the command line, you can pass in one or more command line arguments.  In your program, you can access these arguments by defining the Main function to accept a string array as a parameter and then iterating through the array to read the parameters.


1
2
3
4
5
6
7
8
9
10
static void Main(string[] args)
{
    // Ex 1: List all arguments
    foreach (string arg in args)
        Console.WriteLine(string.Format("Arg: [{0}]", arg));
 
    // Ex 2: Use 1st argument as filename
    if (args.Length > 0)
        Console.WriteLine(string.Format("File={0}", args[0]));
}