how to keep a simple c# console app running after end of the application. [duplicate]

你离开我真会死。 提交于 2019-12-25 02:23:15

问题


I have a simple console tool which is written in c#. As soon as the user enters some data by typing and hitting enter key. My tool does it work and prints out some useful information for the user. Since user already hit the enter key, the console app windows is close even if I put Console.WriteLine(); So the user can't read the output information from the console app.

I added Console.WriteLine(); two times to keep the windows open, but it closes the app immediately after the end of the application. Currently, the only way is to run the console app from command line. When the app finishes from the command line. It keeps the console open and then the user can read the application output.

But I want it so that users can open the app by double click from file explorer. It will do it's work and keep the console open for the user. So that the user can read the output. Then he can close by close button.

p.s: Note that I don't want any kind of background running service. For me only important matter to allow the user to read the last output message from the application.


回答1:


Just use Console.ReadKey();

Console.Write(yourOutput);
Console.ReadKey();



回答2:


The simplest way is to add Console.ReadKey(); at the end of your code, so the app will wait for user input before closing.



来源:https://stackoverflow.com/questions/48169507/how-to-keep-a-simple-c-sharp-console-app-running-after-end-of-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!