如何判断应用程序运行于GUI模式还是Command Line2012-01-16 博客园 Allan越来越多的应用程序提供以命令行的方式来运行,通常的做法有两种:单独给应用程序写一个基于命令行运行的控制台程序,用户运行这个程序时它一定是以命令行的方式来运行;以GUI和Command Line共享一个应用或exe文件,但通过不同的arguments来判断,最终分别做不同的处理。对于单独给应用程序写基于命令行运行的控制台程序,无非是通过判断传递的args数组来辨别并设置程序运行所需要的参数,最终设定各项参数而完成所需要的工作。在这里建议提供对于/?的帮助菜单,方便用户查询。
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 for this program 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 the specified project file:"" + args[0] + """); Console.WriteLine("Please check the path of project file and try again."); return false; } }}