Problem solve Get help with specific problems with your technologies, process and projects.

How to display the cmd window when running C# programs

I'm a beginner to C# programming and have what I suspect is a very simple question. When I execute a program, how do I get the program to stop and display the cmd window when my program runs?

In C, we used a line like getchar or something like that, but it doesn't seem to work in C#. Your assistance would be most appreciated.
You can use the System.Console class to read and write to the StdIn/StdOut/StdError streams. Make sure your project type is set to Console Application, and then use Console.Read() or Console.ReadLine() to read input from the command window. These calls are synchronous and will block execution of your application until they return. This will provide the effect you are after if you want the console window to remain open until your data has been input from the command window. Try this snippet inside of your application. Start a new console application in C# and paste this code over the stock Main method.

 static void Main(string[] args)
  // read the ASCII value of the char typed and cast it to a char value
  char ch = (char)Console.Read();

  // read the ASCII value of the char typed

  int asciiCode = Console.Read(); 

  // read the input data until a return or new line is read
  // this allows the user to type away until they press enter/return
  // this method will return you information one line at a time... hence the name! :)
  string line = Console.ReadLine();   

Dig Deeper on C# programming language

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.