#12 – 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.

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]));
}

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #12 – Read Command Line Arguments

  1. msuworld says:

    Hey, Where this files’s are created by default? Or its just showing not creating any file ?

    • Sean says:

      This example is saying that you could pass a filename in as the first parameter and then do something with it. But the example just displays the value of the 1st argument.

  2. msuworld says:

    it’s a lame question plz delete that 🙂

Leave a comment