Hi,@ William Liu. Welcome to Microsoft Q&A.
How do you use AllocConsole/FreeConsole? Is it used like the following code?
public partial class MainWindow : Window
{
[DllImport("Kernel32")]
public static extern void AllocConsole();
[DllImport("Kernel32", SetLastError = true)]
public static extern void FreeConsole();
public MainWindow()
{
InitializeComponent();
OutputTextBox.Focus();
AllocConsole();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var line = OutputTextBox.Text;
Console.Out.WriteLine(line);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
FreeConsole();
}
}
Or you could try the method below to see if it works for you.
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
var handle = GetConsoleWindow();
// Hide
ShowWindow(handle, SW_HIDE);
// Show
ShowWindow(handle, SW_SHOW);
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.