12

I once saw the source code of a winform application and the code had a Console.WriteLine();. I asked the reason for that and i was told that it was for debugging purposes.

Pls what is the essence of Console.WriteLine(); in a winform and what action does it perform because when i tried using it, it never wrote anything.

  • Did you check the Output window? (though, honestly, the author should be using Debug.*/Trace.*) – Brad Christie Aug 27 '13 at 19:31
  • 5
    Change your Winform project type to Console Application (`project/properties/application/output type`) and try again. All in one :) – I4V Aug 27 '13 at 19:31
  • That line of code was probably meant to be temporary. I'd guess it was put there just so the developer could set a break point for debugging. – Crispy Aug 27 '13 at 19:36

4 Answers4

14

It writes to the Console.

The end user won't see it, and to be honest its much cleaner to put it into a proper log, but if you run it through VS the Console window will populate.

Haedrian
  • 4,240
  • 2
  • 32
  • 53
8

Winforms are just console apps that show windows. You can direct your debug info to the console app.

As you can see in the below example there is a command that attaches the parent window then pumps info to it.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MyWinFormsApp
{
    static class Program
    {
        [DllImport( "kernel32.dll" )]
        static extern bool AttachConsole( int dwProcessId );
        private const int ATTACH_PARENT_PROCESS = -1;

        [STAThread]
        static void Main( string[] args )
        {
            // redirect console output to parent process;
            // must be before any calls to Console.WriteLine()
            AttachConsole( ATTACH_PARENT_PROCESS );

            // to demonstrate where the console output is going
            int argCount = args == null ? 0 : args.Length;
            Console.WriteLine( "nYou specified {0} arguments:", argCount );
            for (int i = 0; i < argCount; i++)
            {
                Console.WriteLine( "  {0}", args[i] );
            }

            // launch the WinForms application like normal
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault( false );
            Application.Run( new Form1() );
        }
    }
}

Here is the resource for this example:http://www.csharp411.com/console-output-from-winforms-application/

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • 3
    "Winforms are just console apps that show windows" -- I beg to disagree, and even firmly. There are two kinds of applications in Windows: GUI apps that create windows, and (non-GUI) console applications that run in a character mode console, the best known ones being cmd.exe and Powershell. Console apps *can* create windows by calling the appropriate API's, and GUI apps *can* read and write stdin and stdout as console apps do, but that doesn't mean they're not two entirely different animals. And yes, as others pointed out, Console.WriteLine() in a GUI application is not the recommended way. – Luc VdV Aug 02 '17 at 11:49
  • @LucVdV if you disagree, then post your own answer for the community to benefit from. It would be more useful then commenting on an answer from 4 years ago lol. – DotNetRussell Aug 02 '17 at 12:35
  • I point out an error in your answer, without going as far as downvoting for it. If you prefer a downvote, just say so. – Luc VdV Aug 03 '17 at 14:42
  • @LucVdV It's an open community and you're free to do as you want. I don't care about upvotes or downvotes. Just sharing info with others. If you have input, you should make it an answer so this question will get bumped and your response will be preserved for others to learn from. – DotNetRussell Aug 03 '17 at 14:45
  • 2
    Your first sentence is absolutely not true. There are different subsystems for GUI (subsystem WINDOWS) and console (subsystem CONSOLE) applications. – user3700562 Jun 11 '18 at 12:06
4

You wouldn't really use it normally but if you've attached a Console or use AllocConsole, it will function like in any other console application and the output will be visible there.

For quick debugging, I prefer Debug.WriteLine but for a more robust solution, the Trace class may be preferable.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • 1
    I just use the old standby Debug.Print - the output goes to the Immediate Window which works find for simple code debug. – rheitzman Aug 27 '13 at 21:56
2

It wouldn't perform anything unless the Console was redirected to say the Output window. Really, they should be leveraging Debug.WriteLine instead.

The benefit of Debug.WriteLine is that it gets optimized away when building in Release mode.

NOTE: as pointed out by Brad Christie and Haedrian, apparently it will in fact write to the Console window in Visual Studio when running a Windows Forms application. You learn something new every day!

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232