Why Does Console.WriteLine() Not Write to Command Window?

  • Thread starter Thread starter joey.powell
  • Start date Start date
J

joey.powell

I have a windows form app in C# in VS2005. In the "program.cs" file, I
take some command line arguments and use "Console.WriteLine("fsdfsd")
to give feedback to users. When I step through the program, the code
block is executing, but no text is written to the command line.

What might be causing this?
 
Quite simply, windows apps don't connect to a console.

It can either be console or winform; Not both. This is part of the PE
header so is more Windows than .Net.

You could perhaps have 2 exes (one winform, one console).
Trace.WriteLine can be useful, as this will appear in the debugger by
default, or can be redirected.

Marc
 
Is just a thought but. why you dont try:

console.WriteLine("...");
Application.Run(new Form1);
 
put a breakpoint on the first line of your app and inspect
System.Console.Out. You'll see that it's ultimately a wrapper around
System.IO.Stream.NullStream.

((System.IO.StreamWriter)(((System.IO.TextWriter.SyncTextWriter)(System.Console.Out))._out)).BaseStream

Use a logging framework instead of console.writeline (I'd suggest
log4net).

HTH,

Sam
 
I have a windows form app in C# in VS2005. In the "program.cs" file, I
take some command line arguments and use "Console.WriteLine("fsdfsd")
to give feedback to users. When I step through the program, the code
block is executing, but no text is written to the command line.

What might be causing this?

You've told the compiler that you have a windowed application. If you will
tell the compiler to compile it as a console app (which is somewhere in
Project Properties), you'll get a console window, and the forms will all
still work. This is a handy secret.

Or use System.Diagnostics.Debug.WriteLine(...) to write in the debug window
in VS2005 while the program runs.
 
Marc Gravell said:
Quite simply, windows apps don't connect to a console.

It can either be console or winform; Not both. This is part of the PE
header so is more Windows than .Net.

Little-known fact: Actually it *can* be both. Take a Windows Forms app and
compile it as a console app, and you'll have a console window.

You can also use Win32 calls to create a console and attach a stream to it.
 
Little-known fact: Actually it *can* be both. Take a Windows Forms app and
compile it as a console app, and you'll have a console window.

Not really, it's actually a console application that pops up a window.
The original console window stays visible all the time.
 
Back
Top