There are two common ways to read command line arguments in C#. First, you can override the Main method with an array of strings, which are command line arguments. For example, the following code loops through the command line arguments and print them on the console.
static void Main(string[] args)
{
foreach(string arg in args)
{
Console.WriteLine(arg);
}
Console.ReadLine();
}
You can do so using the Environment class, which has a static method called GetCommandLineArgs, which returns an array of strings containing the arguments. The following code reads the command line arguments using Environment.GetCommandLineArgs method.
foreach (string arg in Environment.GetCommandLineArgs())
{
Console.WriteLine(arg);
}
Tuesday, November 1, 2011
Subscribe to:
Posts (Atom)