This article is a mirror article of machine translation, please click here to jump to the original article.

View: 25008|Reply: 4

[Console Program] C# implements the console application to exit by pressing any key

[Copy link]
Posted on 11/22/2016 2:49:39 PM | | |

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.




Previous:C# binary and interconversion between strings
Next:Recover data that was deleted by mistake in SQL Server
Posted on 6/16/2017 9:19:16 AM |
Console.Write("Press any key to exit...");
Console.ReadKey(true);
Posted on 6/16/2017 1:15:31 PM |
Ctrl+F5 to try...
Posted on 7/1/2017 3:35:09 AM |
Thank you for sharing, thank you for sharing, thank you for sharing, thank you for sharing
Posted on 7/7/2017 6:47:04 AM |
Console.ReadKey(true);
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com