You could try grabbing the console window from a kernel 32 function call.
private const string Kernel32_DllName = "kernel32.dll";
[DllImport(Kernel32_DllName)]
private static extern IntPtr GetConsoleWindow();
public static bool HasConsole {
get { return GetConsoleWindow() != IntPtr.Zero; }
}
HasConsole should return true if there is a console loaded, false if launched from a window.
If the program is a console application, there will always be a console window open, so detecting the launch method is more tricky.
There is an SO question covering this already - How can you determine how a console application was launched?
As a quick reference, the relevant section is listed below, but I would recommend having a read through that thread as it explains the process more clearly.
static bool StartedFromGui =
!Console.IsOutputRedirected
&& !Console.IsInputRedirected
&& !Console.IsErrorRedirected
&& Environment.UserInteractive
&& Environment.CurrentDirectory == System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
&& Console.CursorTop == 0 && Console.CursorLeft == 0
&& Console.Title == Environment.GetCommandLineArgs()[0]
&& Environment.GetCommandLineArgs()[0] == System.Reflection.Assembly.GetEntryAssembly().Location;