命令行参数(C# 编程指南)

通过以下方式之一定义方法,可以将参数发送至 Main 方法。

static int Main(string[] args)
static void Main(string[] args)
注意
若要在 Windows 窗体应用程序中的 Main 方法中启用命令行参数,必须手动修改 program.cs 中 Main 的签名。Windows 窗体设计器生成的代码创建没有输入参数的 Main。也可以使用 Environment.CommandLineEnvironment.GetCommandLineArgs 从控制台或 Windows 应用程序中的任何位置访问命令行参数。

Main 方法的参数是表示命令行参数的 String 数组。一般是通过测试 Length 属性来确定参数是否存在,例如:

if (args.Length == 0)
{
    System.Console.WriteLine("Please enter a numeric argument.");
    return 1;
}

还可以使用 Convert 类或 Parse 方法将字符串参数转换为数值类型。例如,下面的语句使用 Parse 方法将 string 转换为 long 数字:

long num = Int64.Parse(args[0]);

也可以使用别名为 Int64 的 C# 类型 long:

long num = long.Parse(args[0]);

还可以使用 Convert 类的方法 ToInt64 完成同样的工作:

long num = Convert.ToInt64(s);

有关更多信息,请参见ParseConvert

下面的示例演示如何在控制台应用程序中使用命令行参数。应用程序在运行时采用一个参数,将该参数转换为整数,并计算该数的阶乘。如果没有提供参数,则应用程序发出一条消息来解释程序的正确用法。

若要根据命令提示编译并运行应用程序,请执行以下步骤:

  1. 将以下代码粘贴到任何文本编辑器中,并将文件保存为名为 Factorial.cs 的文本文件。

    //Add a using directive for System if the directive isn't already present.
    
    public class Functions
    {
        public static long Factorial(int n)
        {
            // Test for invalid input
            if ((n < 0) || (n > 20))
            {
                return -1;
            }
    
            // Calculate the factorial iteratively rather than recursively:
            long tempResult = 1;
            for (int i = 1; i <= n; i++)
            {
                tempResult *= i;
            }
            return tempResult;
        }
    }
    
    class MainClass
    {
        static int Main(string[] args)
        {
            // Test if input arguments were supplied:
            if (args.Length == 0)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }
    
            // Try to convert the input arguments to numbers. This will throw
            // an exception if the argument is not a number.
            // num = int.Parse(args[0]);
            int num;
            bool test = int.TryParse(args[0], out num);
            if (test == false)
            {
                System.Console.WriteLine("Please enter a numeric argument.");
                System.Console.WriteLine("Usage: Factorial <num>");
                return 1;
            }
    
            // Calculate factorial.
            long result = Functions.Factorial(num);
    
            // Print result.
            if (result == -1)
                System.Console.WriteLine("Input must be >= 0 and <= 20.");
            else
                System.Console.WriteLine("The Factorial of {0} is {1}.", num, result);
    
            return 0;
        }
    }
    // If 3 is entered on command line, the
    // output reads: The factorial of 3 is 6.
    
  2. 从“开始”屏幕或“开始”菜单中,打开 Visual Studio“开发人员命令提示”窗口,然后导航到包含您刚创建的文件的文件夹。

  3. 若要编译应用程序,请输入下面的命令。

    csc Factorial.cs

    如果您的应用程序中有没有编译错误,则将创建名为 Factorial.exe 的可执行文件。

  4. 输入以下命令来计算 3 的阶乘:

    Factorial 3

  5. 此命令将生成以下输出:The factorial of 3 is 6.

注意
在 Visual Studio 中运行应用程序时,可以在“项目设计器”->“调试”页中指定命令行参数。

有关如何使用命令行参数的更多示例,请参见如何:使用命令行创建和使用程序集(C# 和 Visual Basic)

请参阅

System.Environment

C# 编程指南

Main() 和命令行参数(C# 编程指南)

如何:显示命令行参数(C# 编程指南)

如何:使用 foreach 访问命令行参数(C# 编程指南)

Main() 返回值(C# 编程指南)

类(C# 编程指南)