Ask the Expert

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();   

This was first published in August 2004

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: