0

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.

masiboo
  • 4,537
  • 9
  • 75
  • 136

2 Answers2

4

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

  • 1
    @L.Guthardt: From [help/privileges/comment](https://stackoverflow.com/help/privileges/comment): "**When shouldn't I comment?** Answering a question or providing an alternate solution to an existing answer; instead, post an actual answer (or edit to expand an existing one); – Zohar Peled Jan 09 '18 at 13:48
  • 1
    @L.Guthardt if its an answer it should be posted as an answer, not a comment. "Comments exist so that users can talk [...] without [...] an attempt to answer the question asked" (from [How do comments work?](https://meta.stackexchange.com/a/19757/316218)) – appa yip yip Jan 09 '18 at 13:49
  • @ZoharPeled To both of you guys, agreed. Technically it is an answer, but since you all know that *this* question exists several times on SO, there is no need to go for an answer, since it's supposed to get closed. In my opinion it's unnecessary and just seems quite rep greedy. – L. Guthardt Jan 09 '18 at 13:55
  • 1
    @L.Guthardt, go away. Your comments are far more disruptive than their answers. – James Hill Jan 09 '18 at 14:00
  • 1
    @L.Guthardt even duplicate questions sometimes deserves answers. And why would you care if someone gets some reputation from it? Moreover, the fact that a question is a duplicate does not guarantee it will be closed. In fact, Just today I've marked a duplicated question (that got closed eventually), and it was answered by a moderator. I also upvoted the answer and even commented on it with improvement suggestions. – Zohar Peled Jan 09 '18 at 14:05
  • @JamesHill Hmm "disruptive", it's just my opinion. Which lead to an explaination why you think that those answers are legit. The purpose was simply to understand the legitimation for the existence of answers given to a duplicate question. – L. Guthardt Jan 09 '18 at 14:58
  • Thanks to @ZoharPeled I get the point. Thanks for the explaination, will take your behaviour as a model. :-) – L. Guthardt Jan 09 '18 at 14:59
0

Just use Console.ReadKey();

Console.Write(yourOutput);
Console.ReadKey();
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47
  • Could have been simply added as a comment, no need to use an answer for a few lines of text. – L. Guthardt Jan 09 '18 at 13:45
  • 3
    @L.Guthardt, this is an answer. It should definitely go in the "answer" section, not the comments. – Matt Spinks Jan 09 '18 at 13:48
  • Technically it is an answer, but since you all know that *this* question exists several times on SO, there is no need to go for an answer, since it's supposed to get closed. – L. Guthardt Jan 09 '18 at 13:59
  • 3
    @L.Guthardt, what's your point here? An answer is an answer. And who is "you all"? It's just me here. – Matt Spinks Jan 09 '18 at 14:04
  • *You all* is meant to be the SO community. – L. Guthardt Jan 09 '18 at 14:53
  • 1
    If you are trying to say something to the entire SO community, a comment on my answer is not the place to do it, as not many people will see it. Why not try meta stack overflow? – Matt Spinks Jan 09 '18 at 15:03