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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11512821/
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-08-09 18:08:45  来源:igfitidea点击:

How to stop C# console applications from closing automatically?

c#console-application

提问by Zignd

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")在执行结束时“暂停”应用程序,我该如何实现?

采纳答案by Silvia Z

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.

尝试一下。我总是这样做,控制台会显示我在上面打开的结果。不需要额外的代码。

回答by Darin Dimitrov

Console.ReadLine()to wait for the user to Enteror Console.ReadKeyto wait for any key.

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

回答by Adam

Console.ReadLine();

or

或者

Console.ReadKey();

ReadLine()waits for ?, ReadKey()waits for any key (except for modifier keys).

ReadLine()waits for ?ReadKey()等待任何键(修饰键除外)。

Edit: stole the key symbol from Darin.

编辑:从达林那里偷了钥匙符号。

回答by matthewr

Use:

用:

Console.ReadKey();

For it to close when someone presses any key, or:

当有人按任意键时它会关闭,或者:

Console.ReadLine();

For when the user types something and presses enter.

当用户输入内容并按下回车键时。

回答by Sohail xIN3N

Try Ctrl + F5in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console.Readline() or ReadKey() functions.

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

回答by gotqn

Alternatively, you can delay the closing using the following code:

或者,您可以使用以下代码延迟关闭:

System.Threading.Thread.Sleep(1000);

Note the Sleepis using milliseconds.

请注意Sleep使用毫秒。

回答by Slobodan Stankovi?

Ctrl + F5 is better, because you don't need additional lines. And you can, in the end, hit enter and exit running mode.

Ctrl + F5 更好,因为您不需要额外的行。最后,您可以点击进入和退出运行模式。

But, when you start a program with F5 and put a break-point, you can debug your application and that gives you other advantages.

但是,当您使用 F5 启动程序并设置断点时,您可以调试您的应用程序,这会给您带来其他优势。

回答by Eric

Those solutions mentioned change how your program work.

提到的那些解决方案会改变您的程序的工作方式。

You can off course put #if DEBUGand #endifaround the Console calls, but if you really want to prevent the window from closing only on your dev machine under Visual Studio or if VS isn't running only if you explicitly configure it, and you don't want the annoying 'Press any key to exit...'when running from the command line, the way to go is to use the System.Diagnostics.DebuggerAPI's.

您当然可以放置#if DEBUG#endif绕过控制台调用,但是如果您真的想阻止窗口仅在 Visual Studio 下的开发机器上关闭,或者如果 VS 仅在您明确配置它时才运行,并且您不想要'Press any key to exit...'从命令行运行时很烦人,要走的路是使用System.Diagnostics.DebuggerAPI。

If you only want that to work in DEBUG, simply wrap this code in a [Conditional("DEBUG")] void BreakConditional()method.

如果您只希望它在 中工作DEBUG,只需将此代码包装在一个[Conditional("DEBUG")] void BreakConditional()方法中即可。

// Test some configuration option or another
bool launch;
var env = Environment.GetEnvironmentVariable("LAUNCH_DEBUGGER_IF_NOT_ATTACHED");
if (!bool.TryParse(env, out launch))
    launch = false;

// Break either if a debugger is already attached, or if configured to launch
if (launch || Debugger.IsAttached) {
    if (Debugger.IsAttached || Debugger.Launch())
        Debugger.Break();
}

This also works to debug programs that need elevated privileges, or that need to be able to elevate themselves.

这也适用于调试需要提升权限或需要能够提升自己的程序。

回答by PodTech.io

If you do not want the program to close even if a user presses anykey;

如果您不希望即使用户按任意键也关闭程序;

 while (true) {
      System.Console.ReadKey();                
 };//This wont stop app