How does VS compile console applications to show “

2019-01-15 13:35发布

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.

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.

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()?

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

7条回答
该账号已被封号
2楼-- · 2019-01-15 14:00

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

查看更多
乱世女痞
3楼-- · 2019-01-15 14:03

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);
}
查看更多
Deceive 欺骗
4楼-- · 2019-01-15 14:13

You could write a batch script that runs the exe for you and prompts the user to press a key.

The batch script would look something like this:

echo off
YourApp.exe
pause
查看更多
▲ chillily
5楼-- · 2019-01-15 14:17

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.

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.

查看更多
我命由我不由天
6楼-- · 2019-01-15 14:18

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.

查看更多
We Are One
7楼-- · 2019-01-15 14:19

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.

查看更多
登录 后发表回答