November 14, 2019

Console Application Template

Here is a simple template for console applications, including how to change the font color within the console

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("Started SomeConsoleApplication");
    Console.WriteLine("");

    Console.ForegroundColor = ConsoleColor.Green; // Go/all is good colour
    try
    {
      // Do your console app work here
      SqlVersionFinder sqlVersionFinder = new SqlVersionFinder();
      var res = sqlVersionFinder.GetSqlVersion();
      Console.WriteLine("Version:   " + res.version);
      Console.WriteLine("Date:      " + res.date);
      Console.WriteLine("Copyright: " + res.copyright);
      Console.WriteLine("Edition:   " + res.edition);

    }
    catch (Exception ex)
    {
      Console.WriteLine("Oops, an exception occurred trying to run the application: "
                                  + ex.ToString());
      Console.ForegroundColor = ConsoleColor.Red; // Error colour
      Console.ResetColor();
    }

    Console.ResetColor();
    // This will stop the console application from finishing until a key is pressed
    // However you might not want this if the application is to be run from another
    // process or you might want to use a command line argument to enable or 
    // disable this option
    Console.WriteLine("");
    Console.WriteLine("Press any key to finish ...");
    Console.ReadKey(); 
  }
}

No comments: