本示例演示如何访问命令行,并演示访问命令行参数数组的两种方法。
// cmdline1.cs
// 参数:A B C
using System;
public class CommandLine
{
public static void Main(string[] args)
{
// Length 属性用于获取数组的长度。
// 注意,Length 是只读属性:
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
for(int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
}
}
}
// cmdline2.cs
// 参数:John Paul Mary
using System;
public class CommandLine2
{
public static void Main(string[] args)
{
Console.WriteLine("Number of command line parameters = {0}",
args.Length);
foreach(string s in args)
{
Console.WriteLine(s);
}
}
}
