#12 – Read Command Line Arguments
June 29, 2010 Leave a comment
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]));
}