Tuesday 17 May 2016

Change Console Foreground Color, Background Color and Title in C#


This post explains how to change foreground color, background color and title of  console window in C#.

In some cases it is required to change the text color in console window depending on the context of application. For example, green color text indicating success or red color text indicating error/warning.

Here is the sample code -

Create a new project of type console application with a name "ConsoleDemoApp" and enter the code in Program.cs as shown here.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Threading;

namespace ConsoleDemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // We want to save the current console color and background color so we can restore it later
            ConsoleColor oldColor = Console.ForegroundColor;
            ConsoleColor oldBackgroundColor = Console.BackgroundColor;            

            // Set the new console color to Green and background color to Gray
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.DarkGray;
                        
            //set title of console window
            //If you don't set the title, the console window shows the fullpath of .exe in title
            Console.Title = "Console Demo Application";

            // Tell everyone what this program is when it starts
            Console.WriteLine("Welcome to Demo Application");

            Random random = new Random();
            while (true)
            {
                Console.Write("Ask any question: ");
                string question = Console.ReadLine();
                //console window exits until u enter either quit or exit
                if (question.ToLower() == "quit" || question.ToLower() == "exit")
                {
                    break;
                }
                Console.Write("Answer: ");
                switch( random.Next(4) )
                {
                    case 0:
                        Console.WriteLine("Yes!");
                        break;
                    case 1:
                        Console.WriteLine("No!");
                        break;
                    case 2:
                        Console.WriteLine("Probably.");
                        break;
                    case 3:
                        Console.WriteLine("I don't understand!!!");
                        break;
                }
            }

            // Restore the old foreground color and background color
            Console.ForegroundColor = oldColor;
            Console.BackgroundColor = oldBackgroundColor;
        }
    }
}

Output -



Karthik Byggari

Author & Editor

Computer Science graduate, Techie, Founder of logicallyproven, Love to Share and Read About pprogramming related things.

0 comments:

Post a Comment

 
biz.