How to stop console from closing on exit? [duplica

2019-01-13 04:38发布

This question already has an answer here:

I'm using Visual Studio 2010 and Windows 7 x64

The command prompt closes after exit, even though I used "Start without debug". Is there a setting somewhere that I can use?

5条回答
神经病院院长
2楼-- · 2019-01-13 04:44

You can simply press Ctrl+F5 instead of F5 to run the built code. Then it will prompt you to press any key to continue. Or you can use this line -> system("pause"); at the end of the code to make it wait until you press any key.

However, if you use the above line, system("pause"); and press Ctrl+F5 to run, it will prompt you twice!

查看更多
做自己的国王
3楼-- · 2019-01-13 04:45

You could open a command prompt, CD to the Debug or Release folder, and type the name of your exe. When I suggest this to people they think it is a lot of work, but here are the bare minimum clicks and keystrokes for this:

  • in Visual Studio, right click your project in Solution Explorer or the tab with the file name if you have a file in the solution open, and choose Open Containing Folder or Open in Windows Explorer
  • in the resulting Windows Explorer window, double-click your way to the folder with the exe
  • Shift-right-click in the background of the explorer window and choose Open Commmand Window here
  • type the first letter of your executable and press tab until the full name appears
  • press enter

I think that's 14 keystrokes and clicks (counting shift-right-click as two for example) which really isn't much. Once you have the command prompt, of course, running it again is just up-arrow, enter.

查看更多
倾城 Initia
4楼-- · 2019-01-13 04:51

Add a Console.ReadKey call to your program to force it to wait for you to press a key before exiting.

查看更多
萌系小妹纸
5楼-- · 2019-01-13 04:53

Yes, in VS2010 they changed this behavior somewhy.
Open your project and navigate to the following menu: Project -> YourProjectName Properties -> Configuration Properties -> Linker -> System. There in the field SubSystem use the drop-down to select Console (/SUBSYSTEM:CONSOLE) and apply the change.
"Start without debugging" should do the right thing now.

Or, if you write in C++ or in C, put

system("pause");

at the end of your program, then you'll get "Press any key to continue..." even when running in debug mode.

查看更多
来,给爷笑一个
6楼-- · 2019-01-13 05:07

What about Console.Readline();?

查看更多
登录 后发表回答