visual-studio 如何阻止控制台在退出时关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4118073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:42:49  来源:igfitidea点击:

How to stop console from closing on exit?

visual-studiowindows-7

提问by segfault

I'm using Visual Studio 2010 and Windows 7 x64

我使用的是 Visual Studio 2010 和 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?

命令提示符在退出后关闭,即使我使用了“不调试启动”。是否有我可以使用的设置?

回答by Suhas Bharadwaj

You can simply press Ctrl+F5instead of F5to 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.

您可以简单地按Ctrl+F5而不是F5运行构建的代码。然后它会提示你按任意键继续。或者您可以system("pause");在代码末尾使用此行 ->使其等待直到您按下任意键。

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

但是,如果您使用上面的行,system("pause");然后按Ctrl+F5运行,它会提示您两次!

回答by lapis

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.

是的,在 VS2010 中,他们以某种方式改变了这种行为。
打开您的项目并导航到以下菜单:项目 -> YourProjectName 属性 -> 配置属性 -> 链接器 -> 系统。在 SubSystem 字段中,使用下拉菜单选择 Console (/SUBSYSTEM:CONSOLE) 并应用更改。
“不调试就开始”现在应该做正确的事情。

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

或者,如果你用 C++ 或 C 编写,把

system("pause");

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

在程序结束时,即使在调试模式下运行,您也会收到“按任意键继续...”。

回答by Rahul Soni

What about Console.Readline();?

怎么样Console.Readline();

回答by SLaks

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

Console.ReadKey向您的程序添加一个调用以强制它在退出之前等待您按下某个键。

回答by Kate Gregory

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:

您可以打开命令提示符、CD 到 Debug 或 Release 文件夹,然后键入您的 exe 的名称。当我向人们提出这个建议时,他们认为这需要大量的工作,但这里有最少的点击次数和击键次数:

  • 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
  • 在 Visual Studio 中,右键单击解决方案资源管理器中的项目或具有文件名的选项卡(如果解决方案中有文件打开),然后选择打开包含文件夹或在 Windows 资源管理器中打开
  • 在生成的 Windows 资源管理器窗口中,双击带有 exe 的文件夹
  • 在资源管理器窗口的背景中按住 Shift 并右键单击,然后选择在此处打开命令窗口
  • 键入可执行文件的第一个字母并按 Tab 键直到出现全名
  • 按回车

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.

我认为这是 14 次击键和点击(例如将 shift-right-click 计算为两次),这真的不多。一旦你有了命令提示符,当然,再次运行它只是向上箭头,回车。