0

I have a C# Winform application. I want to start the application from the console (i.e. start the .exe from command prompt) and write some messages on it.

    static void Main(string[] args)
    {
        string cmfile = string.Empty;

        if (args.Length == 1)
            cmfile = args[0];

        Console.WriteLine("I want to read this line on CONSOLE");
        MessageBox.Show("This is my Message", "Window Name"); //Just to PAUSE the program so that i can read the message written on the console in the line above

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm(cmfile));
    }

QUESTION: In the above code snippet, I want to see the string (i.e. "I want to read this line on CONSOLE") gets written on the console window, which started the .exe. How can I do that?

user2756695
  • 676
  • 1
  • 7
  • 22
  • Does this answer your question? [How do I show a console output/window in a forms application?](https://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application) – Martheen Jun 30 '20 at 18:26

2 Answers2

0

How are you running the executable? Console.WriteLine will output to the console the executable is running on.

See example below https://www.programiz.com/csharp-programming/hello-world

// Hello World! program
namespace HelloWorld
{
    class Hello {         
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}
  • I already mentioned in the OP that I want to write on the Console window which starts the program. Your answer will not work because my application is a WinForm application. – user2756695 Jul 01 '20 at 07:54
0

My bad. I do see that at the top. Try this please.


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() );
        }
    }
}

  • Unfortunately, this also did not work. I found some other answers on SO which says the same that this method is not reliable. – user2756695 Jul 02 '20 at 08:47