How does VS compile console applications to show "Press any key to continue"?

It's nothing to do with the compiler - if you press F5 to debug it rather than Ctrl-F5 to run without debugging, then VS doesn't show the prompt. This is presumably so that you don't miss whatever output it's producing.

In order to do this, Visual Studio runs cmd.exe telling it to run your executable and then pause:

"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.

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.


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.

The only way is by using Console.Read() in your code

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.


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'.