C# 如何保持控制台窗口打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16952846/
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
How to keep console window open
提问by user1929393
When I run my program, the console window seems to run and close. How to keep it open so I can see the results?
当我运行我的程序时,控制台窗口似乎运行并关闭。如何保持打开状态以便我可以看到结果?
class Program
{
public class StringAddString
{
public virtual void AddString()
{
var strings2 = new string[] { "1", "2", "3", "4", "5","6", "7", "8", "9"};
Console.WriteLine(strings2);
Console.ReadLine();
}
}
static void Main(string[] args)
{
StringAddString s = new StringAddString();
}
}
采纳答案by cuongle
You forgot calling your method:
你忘了调用你的方法:
static void Main(string[] args)
{
StringAddString s = new StringAddString();
s.AddString();
}
it should stop your console, but the result might not be what you expected, you should change your code a little bit:
它应该停止您的控制台,但结果可能不是您所期望的,您应该稍微更改您的代码:
Console.WriteLine(string.Join(",", strings2));
回答by TGH
Put a Console.Read() as the last line in your program. That will prevent it from closing until you press a key
将 Console.Read() 作为程序的最后一行。这将阻止它关闭,直到你按下一个键
static void Main(string[] args)
{
StringAddString s = new StringAddString();
Console.Read();
}
回答by Keith Nicholas
There are two ways I know of
我知道有两种方式
1) Console.ReadLine()at the end of the program. Disadvantage, you have to change your code and have to remember to take it out
1)Console.ReadLine()在程序结束时。缺点,你要改你的代码,还得记得把它拿出来
2) Run outside of the debugger CONTROL-F5this opens a console window outside of visual studio and that window won't close when finished. Advantage, you don't have to change your code. Disadvantage, if there is an exception, it won't drop into the debugger (however when you do get exceptions, you can simply just rerun it in the debugger)
2) 在调试器之外运行,CONTROL-F5这会在 Visual Studio 之外打开一个控制台窗口,完成后该窗口不会关闭。优点,您不必更改代码。缺点,如果有异常,它不会掉到调试器中(但是当你得到异常时,你可以简单地在调试器中重新运行它)
回答by Sarvesh Nayak
Use Console.Readline() at the end .Your code will not close until you close it manually.Since Readline waits for input that needs to be entered for your code hence your console will be open until you type some input.
最后使用 Console.Readline() 。直到您手动关闭它,您的代码才会关闭。由于 Readline 等待需要为您的代码输入的输入,因此您的控制台将打开,直到您输入一些输入。
回答by Edward Karak
Make sure to useConsole.ReadLine();to keep the preceeding Console.WriteLine("");message from closing.
确保使用Console.ReadLine();以防止前面的Console.WriteLine("");消息关闭。
回答by Herr Grumps
If you want to keep it open when you are debugging, but still let it close normally when not debugging, you can do something like this:
如果你想在调试的时候让它保持打开状态,但在不调试的时候仍然让它正常关闭,你可以这样做:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadLine();
Like other answers have stated, the call to Console.ReadLine()will keep the window open until enter is pressed, but Console.ReadLine()will only be called if the debugger is attached.
与其他答案一样,调用 toConsole.ReadLine()将使窗口保持打开状态,直到按下 Enter 为止,但Console.ReadLine()只有在连接了调试器时才会调用。
回答by Cpt Balu
Console.ReadKey(true);
This command is a bit nicer than readline which passes only when you hit enter, and the true parameter also hides the ugly flashing cursor while reading the result :) then any keystroke terminates
这个命令比 readline 好一点,它只在你按下回车键时才通过,并且 true 参数在读取结果时也会隐藏丑陋的闪烁光标:) 然后任何击键终止
回答by Mansuu....
Write Console.ReadKey();in the last line of main()method. This line prevents finishing the console. I hope it would help you.
写Console.ReadKey();在main()方法的最后一行。此行阻止完成控制台。我希望它能帮助你。
回答by Charith
You can handle this without requiring a user input.
您无需用户输入即可处理此问题。
Step 1. Create a ManualRestEvent at the start of Main thread
Step 1. 在主线程开始创建一个 ManualRestEvent
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
Step 2 . Wait ManualResetEvent
第2步 。等待手动重置事件
manualResetEvent.WaitOne();
Step 3.To Stop
步骤 3.停止
manualResetEvent.Set();
回答by MD Rakibuz Sultan
For visual c# console Application use:
对于 Visual c# 控制台应用程序使用:
Console.ReadLine();
Console.Read();
Console.ReadKey(true);
for visual c++ win32 console application use:
对于 Visual C++ win32 控制台应用程序使用:
system("pause");
press ctrl+f5 to run the application.
按 ctrl+f5 运行应用程序。

