More and more applications are provided to run on a command-line basis, there are two common approaches: Write a console program that runs on a command line for the application alone, and it must run on a command-line basis when the user runs the program, and share an application or EXE file with the GUI and command line. But through the different arguments to judge, finally does the different processing separately.
For the console program that writes the application based on the command line alone, it is simply to identify and set the parameters required for the program to run by judging the args array passed, and finally set the parameters to complete the required work. The Help menu for/? is recommended here for easy user inquiries.
if (args. Length!= 1)
{
Console.WriteLine ("Error:invalid Argument.");
Console.WriteLine (@ "Type ' MyConsole.exe/? ' for usage.");
return false;
}
else
{
if (args[0] = = @ "/?")
{
Console.WriteLine ("The syntax is:");
Console.WriteLine ("MyConsole.exe [Project file]");
Console.WriteLine ();
Console.WriteLine ("[Project file] The path to the XML project file.");
return false;
else
{
string strfilepath = Args[0];
if (file.exists (strFilePath))
{
...
return true;
else
{
Console.WriteLine ("Can not find" specified project file: ' "+ args[0] +" ");
Console.WriteLine ("Please check the path of project file and try again.");
return false;
}
}