Thursday, 28 November 2013

Create a Simple Console Application

Console applications are ideal for small tools and utilities where a graphical user interface is unnecessary. With console applications primary input is via the keyboard, either by requesting lines of text or detecting individual key presses.

For testing purposes it’s helpful to create a simple application that can read from or write to a console window.

You create a console application using Visual Studio by selecting the Console Application template when creating a new project.

Console class

The Console class contains a set of static methods and properties that allow you to interact with the user in a console application.

Reading a line of text

To receive this type of input, you can use the ReadLine method. ReadLine creates a buffer that can receive up to 254 characters. As the user types, the buffer is updated. When the user presses Enter, the buffer's content is returned as a string. At this point the program continues from the command following the ReadLine.

Reading a single character

Sometimes you'll only want the user to provide a single key press. For example, you might ask a Yes/No question and wait for the user to press either Y or N in response. For this type of operation you can use the ReadKey method. ReadKey waits for the user to press a key, or a key combination including one or more of the control, alt and shift keys. The user does not need to press Enter afterwards for the method to return.

ReadKey returns a value of the ConsoleKeyInfo structure. This structure includes several properties that permit you to understand which key was pressed. For simple operations you can check the KeyChar property. This returns the character that the single key or keyboard combination produces. If you need more control, you can use the Key and Modifiers properties.

Key returns a ConsoleKey value. ConsoleKey is an enumeration containing constants for all of the keys on a standard keyboard that produce input normally. This includes all letters, numbers and symbols and some control keys such as delete, backspace and return. It doesn't include non-input keys such as control, alt and shift. However, you can check the state of these with the Modifiers property.

Modifiers returns a ConsoleModifiers value. This is another enumeration with values for Control, Alt and Shift. It is a bit field enumeration, so you can detect which of the three keys, if any, was used.

How to stop C# console applications closing automatically?

Your console application is closed when it finishes executing all lines in your program. So if you want the application to wait until you press enter then add a Console.ReadLine() statement at the end. If you want the app to be closed for any keystroke then use Console.ReadKey()

A simple console application

To run this code create a console application using Visual Studio and replace Program.cs file's contents with below code.

using System;

namespace SimpleConsoleApp
    class Program
        static void Main(string[] args)
            Console.WriteLine("Type-in some text and press Enter");
            string yourText = Console.ReadLine();
            Console.WriteLine("You entered: {0}", yourText);

            Console.WriteLine("Press any key to continue");

            ConsoleKeyInfo input = Console.ReadKey();


            Console.WriteLine("Character pressed: {0}", 
            Console.WriteLine("Modifiers Used: {0}", 
            Console.WriteLine("Key Used: {0}", input.Key);

            Console.WriteLine("Press any key to exit");

No comments:

Post a Comment