P \ Invoke:

[DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AllocConsole(); 

Call:

 if (AllocConsole()) { // Этот блок выполняется, но текст в консоль не выводится Console.WriteLine("Console Runned!"); } 

ps VS 2017RC, when you call not from under VS, everything works stably. What is the reason?

    1 answer 1

    Perhaps for some reason the console has already been initialized earlier. For example, you have already tried to bring something into it. Or simply looked in the debugger property Console.Out .

    In order to re-initialize the console - you can use two methods.

    Method one - manually:

     Console.SetOut(new StreamWriter(Console.OpenStandartOutput(), Console.OutputEncoding)); 

    Method two is to change the console encoding:

     Console.OutputEncoding = Console.OutputEncoding; 
    • It did not help. I will try to explain in more detail. There is a WinForms application, the console is invoked in it with the help of p \ invoke. The problem takes place only when starting / debugging from the studio. - User12983791