0

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

4

2 回答 2

1

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

在此处输入图像描述

main 方法类似于以下代码段:

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

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

于 2013-07-03T19:27:21.693 回答
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)
}
于 2013-07-03T19:31:40.407 回答