Q

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.

 [STAThread]
 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

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close