简体   繁体   中英

How to write a program in C# that can take arguments

I've seen alot of command-line programs that take arguments, like ping google.com -t . How can I make a program like ping? I would like my program to take a number as an argument and then further use this number: For example: geturi -n 1188

Just write a generic, console application.

在此处输入图片说明

The main method looks like the following snippet:

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

Your arguments are included in the args array.

With a normal Console Application, in static void Main(string[] args) , simply use the args . If you want to read the first argument as a number, then you simply use:

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM