C# - 如何使用起始参数

时间:2017-08-04 10:19:47

标签: c#

我想在启动程序时使用启动参数。例如:

c:\my.exe -arg1

像某些程序一样使用以下参数:

-silent

如果搜索信息但找不到任何帮助。 谢谢!

1 个答案:

答案 0 :(得分:1)

在C#中的方法

public static void Main(string[] args) 

从命令提示参数获取此array of strings - args

例如:

public static class Program
{
    public static void Main(string[] args)
    {
        foreach(var arg in args)
        {
            Console.WriteLine("You entered parameter: " + arg);
        }

        Console.ReadLine();
    }
}

尝试通过以下方式编译和运行此程序:

program Hello World

输出将是:

You entered parameter: Hello
You entered parameter: World