如何设置 C# 应用程序接受命令行参数?

How to set C# application to accept Command Line Parameters?

我创建了一个 C# 命令行应用程序,可以自动为我的客户创建一些报告。然而,即使他们使用 95% 的相同代码,我也想将它们分成不同的进程。我正在使用 Windows 任务调度程序来 运行 它们。如何设置 C# 应用程序以在 运行 时间接受命令行参数?

我在互联网上找不到任何解释。

MSDN

下面的代码片段将打印从命令行传递的参数数量...string[] args 包含参数值...

class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}

所有命令行参数都通过string[] args参数传递给您的应用程序。
下面的代码显示了一个示例:

using System.Linq;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Contains("/REPORT1"))
            {
                /* Do something */
            }
            else if (args.Contains("/REPORT2"))
            {
                /* Do something */
            }
        }
    }
}

然后,在命令提示符下使用:

C:\MyApp\MyApp.exe /REPORT1