C console with parameter program source code preparation example

  • 2020-05-09 19:06:58
  • OfStack

The CMD command like ipconfig /all must be known to all, but many children may not know how to write such a console with the parameters of the program, in fact, it is very simple, let's look at the default code to create the project:
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace _1 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
} 
} 
} 

Ok. We can see an string[] args array, which is used to store the parameters of the program. The parameters are separated by Spaces, representing 1 parameter. If there are Spaces in the parameters, they are enclosed by "parameter parameters", with double quotation marks, such as seay.exe "kill you".
Ok, let's write an example:
 
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( "Please enter parameters  -a -v \ " a s\ "   " ); 
} 
else 
{ 
foreach (string key in args) 
{ 
if (key ==  " a s " ) 
{ 
Console.WriteLine( " This is  ' a s' parameters " ); 
} 
else if (key ==  " -a " ) 
{ 
Console.WriteLine( " This is  ' a' parameters " ); 
} 
else if (key ==  " -v " ) 
{ 
Console.WriteLine( " This is  ' v' parameters " ); 
} 
else 
{ 
Console.WriteLine( "Parameter error" ); 
} 
} 
} 
} 
} 
} 

The above code makes it clear that we can perform the corresponding operations according to the parameters passed in

Related articles: