so all the console output shows up oddly after the prompt
Using AttachConsole() just doesn't work that well in practice. The command processor looks at your .exe file and does two different things, depending if it is a console mode app or a GUI app. If it is console mode app then it waits for the program to exit, then displays the prompt. If it is a GUI app then it doesn't wait, it assumes that the program will display its own window.
Which is what happened, it displayed the prompt after starting your program and waits for input. Your Console.Write() output now intermingles with the command processor output. You'll also have a problem with Console.ReadLine(), the first line that the user types goes to the command processor, not to you. Greatly confuzzling the user.
The workaround is to start your program differently. You have type
start /wait yourapp.exe args...
Which makes the command processor wait for your program to exit, just like it would for a console mode app.
This is not very practical, the user of your app just won't think to use start /wait
. No simple fix for this, you'll need to use AllocConsole(). If that's a deal breaker then you'll just need to create another little EXE project that's a console app. Which starts your GUI app, passing the command line. If you give it the same name as your GUI exe but give the .com
filename extension then it will always be found first. It can look like this, works for any gui app:
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
class Program {
static void Main(string[] args) {
try {
var path = Assembly.GetExecutingAssembly().Location;
var dir = Path.GetDirectoryName(path);
var file = Path.GetFileNameWithoutExtension(path);
path = Path.Combine(dir, file + ".exe");
var prc = Process.Start(path, Environment.CommandLine);
if (args.Length > 0) prc.WaitForExit();
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
Environment.Exit(1);
}
}
}