如何在C#中编写可以接受参数的程序

时间:2013-07-03 19:23:49

标签: c# .net arguments

我见过很多带参数的命令行程序,比如 ping google.com -t 。我怎样才能制作像ping这样的程序?我希望我的程序将一个数字作为参数,然后进一步使用这个数字:     例如: geturi -n 1188

2 个答案:

答案 0 :(得分:1)

只需编写一个通用的控制台应用程序。

enter image description here

主要方法如下所示:

class Program
{
    static void Main(string[] args)
    {
    }
}

您的参数包含在args数组中。

答案 1 :(得分:0)

使用普通的控制台应用程序,在static void Main(string[] args)中,只需使用args即可。如果您想将第一个参数作为数字读取,那么您只需使用:

static void Main(string[] args)
{
    if (args.Length > 1)
    {
        int arg;
        if (int.TryParse(args[0], out arg))
            // use arg
        else // show an error message (the input was not a number)
    }
    else // show an error message (there was no input)
}