C# VS 如何编译控制台应用程序以显示“按任意键继续”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1103402/
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 does VS compile console applications to show "Press any key to continue"?
提问by Moayad Mardini
When I develop a C# console application (which will run on a server) and I run it using Visual Studio, I get a "Press any key to continue" message before the program terminates.
当我开发一个 C# 控制台应用程序(它将在服务器上运行)并使用 Visual Studio 运行它时,我在程序终止之前收到一条“按任意键继续”消息。
However, when I compile the very same C# code file manually using CSC, my program doesn't show that message and it terminates immediately after finishing its logic.
但是,当我使用 CSC 手动编译完全相同的 C# 代码文件时,我的程序不会显示该消息,并且在完成其逻辑后立即终止。
Does anyone know how I can make the same feature when compiling the code without using VS and WITHOUT changing the C# code any adding a ReadLine()?
有谁知道在不使用 VS 和不更改 C# 代码或添加 ReadLine() 的情况下编译代码时如何制作相同的功能?
UPDATE : The same message used to appear when I learned C#, I used to use TextPad with CSC, and that message used to appear without adding any Write(Line)/Read(Line) callings
更新:在我学习 C# 时曾经出现过相同的消息,我曾经将 TextPad 与 CSC 一起使用,并且该消息曾经在不添加任何 Write(Line)/Read(Line) 调用的情况下出现
采纳答案by Andrew Hare
You could write a batch script that runs the exe for you and prompts the user to press a key.
您可以编写一个批处理脚本来为您运行 exe 并提示用户按下一个键。
The batch script would look something like this:
批处理脚本如下所示:
echo off
YourApp.exe
pause
回答by Philippe Leybaert
That's not possible. The prompt to press any key is generated by Visual Studio when running a console app. It's not part of your program.
那是不可能的。运行控制台应用程序时,Visual Studio 会生成按任意键的提示。它不是您程序的一部分。
The only way is by using Console.Read() in your code
唯一的方法是在你的代码中使用 Console.Read()
UPDATE: concerning your remark on using TextPad: I'm not familiar with TextPad, but I wouldn't be surprised if TextPad did the same thing as Visual Studio when running a console app.
更新:关于您对使用 TextPad 的评论:我不熟悉 TextPad,但如果 TextPad 在运行控制台应用程序时执行与 Visual Studio 相同的操作,我不会感到惊讶。
回答by heavyd
This behavior has nothing to do with the compiler you are using. When you compile with Visual Studio, running the executable outside of Visual Studio actually will perform exactly the same as when you compile with CSC on the command line. Visual Studio (and TextPad) is adding the logic to add the "Press any key to continue" message on the console.
此行为与您使用的编译器无关。当您使用 Visual Studio 编译时,在 Visual Studio 之外运行可执行文件实际上与在命令行上使用 CSC 编译时的性能完全相同。Visual Studio(和 TextPad)正在添加逻辑以在控制台上添加“按任意键继续”消息。
If you want your application to stay open, you will need to do something like Console.ReadLine() to block execution so that your application does not complete its execution.
如果您希望您的应用程序保持打开状态,您将需要执行诸如 Console.ReadLine() 之类的操作来阻止执行,以便您的应用程序无法完成其执行。
回答by Pete Kirkham
It's nothing to do with the compiler - if you press F5to debug it rather than Ctrl-F5to run without debugging, then VS doesn't show the prompt. This is presumably so that you don't miss whatever output it's producing.
这与编译器无关——如果你按下F5调试它而不是Ctrl-F5在没有调试的情况下运行,那么 VS 不会显示提示。这大概是为了让您不会错过它产生的任何输出。
In order to do this, Visual Studio runs cmd.exe telling it to run your executable and then pause:
为了做到这一点,Visual Studio 运行 cmd.exe 告诉它运行你的可执行文件,然后暂停:
"C:\WINDOWS\system32\cmd.exe" /c ""...\ConsoleApplication1.exe" & pause"
It probably doesn't do it when you debug as it's a bit harder to get the process ID of a child of a child process.
调试时它可能不会这样做,因为获取子进程的子进程的进程 ID 有点困难。
To add a similar option to your program, either use a command line switch to tell the application itself to pause, or use a batch file to run it then pause, or use a shortcut with them cmd.exe /c
.
要向您的程序添加类似的选项,请使用命令行开关告诉应用程序本身暂停,或使用批处理文件运行它然后暂停,或使用快捷方式cmd.exe /c
。
回答by Pete Kirkham
You could do this...
你可以这样做...
static void Main(string[] args)
{
#if DEBUG
Console.Read();
#endif
}
That way the program will not wait for the console input when you build your application as a 'Release'.
这样,当您将应用程序构建为“发布”时,程序将不会等待控制台输入。
回答by Salo
The question is why would you want to have this behaviour? The Press any key to continue feature is there so that you can see the output of your application. If on the other hand you build your code and run it from a command prompt (console), this will not close when the application finishes, so you can see the output.
问题是你为什么想要这种行为?Press any key to continue 功能在那里,以便您可以看到应用程序的输出。另一方面,如果您构建代码并从命令提示符(控制台)运行它,则在应用程序完成时这不会关闭,因此您可以看到输出。
As noted above, the Press any key to continue is a feature of the IDE and not related to the code you are writing. The purpose of that feature is to allow you to see the output of you console application.
如上所述,按任意键继续是 IDE 的一项功能,与您正在编写的代码无关。该功能的目的是让您查看控制台应用程序的输出。
回答by Siewers
You could do this, if you want the same functionality when debugging.
如果您在调试时想要相同的功能,您可以这样做。
if (Debugger.IsAttached)
{
Console.WriteLine("Press any key to continue . . . ");
Console.ReadKey(true);
}