CMD commands such as ipconfig/all are well known, but many shoes may not know how to write such a console program with parameters. In fact, it is very simple. Let's first look at the default code for creating a project:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace _ 1
{
Class Program
{
Static void Main (string [] args)
{
}
}
}
Okay. We can see that a string [] args array is used to store program parameters. The parameters are separated by spaces, representing a parameter. If the parameter contains spaces, you can use the "parameter marker" to start with a double index, such as seay.exe "kill you ".
Okay, let's write an instance:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace _ 1
{
Class Program
{
Static void Main (string [] args)
{
If (args. Length <1)
{
Console. WriteLine ("enter the parameter-a-v \" a s \ "");
}
Else
{
Foreach (string key in args)
{
If (key = "a s ")
{
Console. WriteLine ("This is 'a s' parameters ");
}
Else if (key = "-")
{
Console. WriteLine ("This is 'A' parameters ");
}
Else if (key = "-v ")
{
Console. WriteLine ("This is 'V' parameters ");
}
Else
{
Console. WriteLine ("parameter error ");
}
}
}
}
}
}
The code above is clear that we can execute corresponding operations based on the input parameters.