Today I wrote a console program in C# that reads data from a database and displays it. After running, the screen flashes, and the program exits, and the data returned by the program cannot be seen at all. How to pause the program after reading the data? Just added a sentence at the end:
Console.Read();
Now the program is suspended. But when you want to exit the program, you still need to press the enter button. Instead of arbitrary keys, how can we achieve the effect in our common console programs? I thought it was Console.Read(); The method is okay, but the result is still not good. I searched on Baidu but failed, but I didn't expect to find the answer to such a simple question (^_^ maybe everyone is a master and can't encounter this kind of problem). I can only solve it myself, I tried many times with the Read() and ReadLine() methods, and finally found that the Console has a ReadKey() method, and the description of the document is: "Get a character or function key pressed by the user", and there is an overload: one is without parameters, and the pressed key is displayed in the console window; Another form has a bool parameter intercept, which determines whether the pressed keys are displayed in the console window, true means that the pressed keys are not displayed, and vice versa.
Console.Write("Press any key to exit..."); Console.ReadKey(true);
Compile Run .............. ok! Finally done it, write it down, hoping to help friends who have the same problem. |