Æðelstan
Æðelstan

Reputation: 852

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

Upvotes: 0

Views: 169

Answers (2)

Jashaszun
Jashaszun

Reputation: 9270

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

Upvotes: 0

RLH
RLH

Reputation: 15718

Just write a generic, console application.

enter image description here

The main method looks like the following snippet:

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

Your arguments are included in the args array.

Upvotes: 1

Related Questions