I’m currently working on a Windows Application that can be executed as a Forms as well as a Console Application. In this program I’m using the well-known Console.WriteLine() statements to log some stuff to the command prompt when running in console mode. Well, I know there is a much better way e.g. log4net but in this case it’s a sufficient solution.
When I ran the application via the console I was wondering ’cause Console.WriteLine() wrote nothing to the command prompt. Of course, the program is a Windows Application and there’s no plan for a console window. And that’s the cause why Console.WriteLine() is “disabled”.
But finally you can simply “enable” the console window by calling the Win32 API function AttachConsole / FreeConsole. All information for this purpose can be found at pinvoke.net.







0 Responses to “Using Console in Windows Forms Applications”