0

So lately I've been trying to develop code that will generate a Console attached to my WinForm app, and I am able to change the properties and stuff like that, but the only thing that doesn't seem to work for me is when I invoke Console.WriteLine("Hello World"); or Console.Write("Hello World");.

Really all I need help with is getting the Console application to allow the app or client to read and write to the Console's output.

My code so far:

public partial class Form1 : Form
{
    public Form1
    {
        InitializeComponent();
    }
    
    [DllImport("kernel32", EntryPoint = "AllocConsole")]
    private static extern bool AttachConsole();
    
    [DllImport("kernel32", EntryPoint = "FreeConsole")]
    private static extern bool CloseConsole();
    
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        
        if (AttachConsole())
        {
            Console.Title = "Hello World";
            Console.WriteLine("Hello World!");
            Console.Out.WriteLine("Hello World!");
            
            Timer t = new Timer { Interval = 2500 };
            t.Tick += (sender, e) =>
            {
                CloseConsole();
            };
        }
    }
}

If anyone could help me figure out what code I need to input, then maybe I might be able to fix this.

0 Answers0