how to run a winform from console application?

reshefm picture reshefm · Nov 10, 2008 · Viewed 115.5k times · Source

How do I create, execute and control a winform from within a console application?

Answer

Marc Gravell picture Marc Gravell · Nov 10, 2008

The easiest option is to start a windows forms project, then change the output-type to Console Application. Alternatively, just add a reference to System.Windows.Forms.dll, and start coding:

using System.Windows.Forms;

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.Run(new Form()); // or whatever
}

The important bit is the [STAThread] on your Main() method, required for full COM support.