Setting up Console.WriteLine & Debug.WriteLine
When I c开发者_开发问答reate a Console Application in Visual Studio, a cmd
Window is automatically provided on which text can be written to and read from. Where is this behaviour set up? Does VS do it or is it merely a consequence of a compiled .Net program?
On a side note, how does VS hook System.Diagnostics.Debug.WriteLine("This is magic");
up to the output Window?
The console behaviour is part of the PE header in the exe; there is a flag to distinguish between console and windows apps. The value of this flag is pretty much the only fundamental difference between a windows exe and a console exe (you can still create forms from a console exe, etc - and you can write to the console from a windows exe, although you won't be able to see anything).
You can toggle this flag using editbin
if you want to play with it - the /subsystem:windows
flag, in particular.
The debug output is because the debugger attaches to pre-defined trace points to gather the output. If you press ctrl+f5 it runs without attaching any of these hooks, so you get something closer to running it directly from the shell.
when your console application is compiled, it creates an .exe file which is a console application. cmd window is invoked if you start that exe, say from windows explorer. If you run that app without debugger, it's essentially the same thing as if you ran it from Run command or windows explorer...
精彩评论