如何阻止C#控制台应用程序自动关闭? [重复]

本文讨论了在C#控制台应用程序执行完成后如何防止窗口立即关闭。提供了使用Console.ReadLine()或Console.ReadKey()方法在程序结束时暂停,以及在Visual Studio中使用Ctrl+F5调试来保持控制台打开的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:How to stop C# console applications from closing automatically? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

My console applications on Visual Studio are closing automatically, so I'd like to use something like C's system("PAUSE") to "pause" the applications at the end of its execution, how can I achieve that? Visual Studio上的我的控制台应用程序正在自动关闭,因此我想使用C system("PAUSE")类的东西在执行结束时“暂停”应用程序,我该如何实现?


#1楼

参考:https://stackoom.com/question/mJ0f/如何阻止C-控制台应用程序自动关闭-重复


#2楼

Console.ReadLine()等待用户进入Console.ReadKey等待任何键。


#3楼

Console.ReadLine();

or 要么

Console.ReadKey();

ReadLine() waits for , ReadKey() waits for any key (except for modifier keys). ReadLine()等待↩, ReadKey()等待的任意键(除修改键)。

Edit: stole the key symbol from Darin. 编辑:偷走了达林的钥匙符号。


#4楼

Use: 使用:

Console.ReadKey();

For it to close when someone presses any key, or: 当有人按任意键时关闭它,或者:

Console.ReadLine();

For when the user types something and presses enter. 当用户输入内容并按下回车键时。


#5楼

You can just compile (start debugging) your work with Ctrl + F5 . 您可以使用Ctrl + F5编译(开始调试)您的工作。

Try it. 试试吧。 I always do it and the console shows me my results open on it. 我总是这样做,控制台告诉我我的结果是打开的。 No additional code is needed. 无需其他代码。


#6楼

在Visual Studio中尝试使用Ctrl + F5来运行程序,这将在没有任何Console.Readline()或ReadKey()函数的情况下自动添加“按任意键继续...”的暂停。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值