It looks like you need to also reallocate the Console
class's input stream if you want to keep allocating a new console and then using ReadLine()
for that new console:
private void button_Click(object sender, RoutedEventArgs e)
{
AllocConsole();
using (Stream stream = Console.OpenStandardInput())
using (TextReader reader = new StreamReader(stream))
{
string x = reader.ReadLine();
}
FreeConsole();
}
That said, I think you're really headed in the wrong direction with this. The console window is an extremely limited means for interacting with the user. It's why we have GUI programs in the first place (Winforms, WPF, etc.). With very little difficulty, and certainly way less difficulty than running into unfamiliar errors related to the mixing of unmanaged calls in your managed program, you can create a window for your program that does everything a console window does, but does it better. IMHO, that's really the right way to go.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…