How do I create, execute and control a winform from within a console application?
10 Answers
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.

- 7,665
- 1
- 29
- 43

- 1,026,079
- 266
- 2,566
- 2,900
-
8@Kenny according to msdn it always has been – Marc Gravell Aug 31 '14 at 21:59
-
1i use vsCode (not visual studio), so i had to add to my csproj file the following xml: `
Exe netcoreapp3.1 true true
I recently wanted to do this and found that I was not happy with any of the answers here.
If you follow Marc's advice and set the output-type to Console Application there are two problems:
1) If you launch the application from Explorer, you get an annoying console window behind your Form which doesn't go away until your program exits. We can mitigate this problem by calling FreeConsole prior to showing the GUI (Application.Run). The annoyance here is that the console window still appears. It immediately goes away, but is there for a moment none-the-less.
2) If you launch it from a console, and display a GUI, the console is blocked until the GUI exits. This is because the console (cmd.exe) thinks it should launch Console apps synchronously and Windows apps asynchronously (the unix equivalent of "myprocess &").
If you leave the output-type as Windows Application, but correctly call AttachConsole, you don't get a second console window when invoked from a console and you don't get the unnecessary console when invoked from Explorer. The correct way to call AttachConsole is to pass -1 to it. This causes our process to attach to the console of our parent process (the console window that launched us).
However, this has two different problems:
1) Because the console launches Windows apps in the background, it immediately displays the prompt and allows further input. On the one hand this is good news, the console is not blocked on your GUI app, but in the case where you want to dump output to the console and never show the GUI, your program's output comes after the prompt and no new prompt is displayed when you're done. This looks a bit confusing, not to mention that your "console app" is running in the background and the user is free to execute other commands while it's running.
2) Stream redirection gets messed up as well, e.g. "myapp some parameters > somefile" fails to redirect. The stream redirection problem requires a significant amount of p/Invoke to fixup the standard handles, but it is solvable.
After many hours of hunting and experimenting, I've come to the conclusion that there is no way to do this perfectly. You simply cannot get all the benefits of both console and window without any side effects. It's a matter of picking which side effects are least annoying for your application's purposes.
-
I'd also add that to get the terminal output in mono of GNU/Linux an usual window application is enough. I.e. `Console.WriteLine` works always by default. – Hi-Angel Aug 28 '15 at 14:46
Here is the best method that I've found: First, set your projects output type to "Windows Application", then P/Invoke AllocConsole to create a console window.
internal static class NativeMethods
{
[DllImport("kernel32.dll")]
internal static extern Boolean AllocConsole();
}
static class Program
{
static void Main(string[] args) {
if (args.Length == 0) {
// run as windows app
Application.EnableVisualStyles();
Application.Run(new Form1());
} else {
// run as console app
NativeMethods.AllocConsole();
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}

- 7,227
- 1
- 35
- 55
-
6-1, creates a second console window and runs the command there. Not what the OP wanted. – tomfanning Sep 27 '11 at 11:20
-
8actually it does `not` create a second console, because you set the project's output to `Windows Application` first, which is admittedly not what the OP asked for, but much likely what he wanted, because that way you dont have the console window when you don't need it – DrCopyPaste Jan 03 '14 at 09:03
-
works as advertised, although i'm not getting the application icon and i don't think the "current working directory" is the same -- any ideas? – drzaus Jan 11 '18 at 17:44
-
"current working directory" is separate issue because I dropped a file onto the exe which apparently changes the cwd. also found a couple more resources for this approach: [possible issue with debug output](https://stackoverflow.com/questions/15604014/no-console-output-when-using-allocconsole-and-target-architecture-x86) and [a way to attach to existing console or create](http://www.pinvoke.net/default.aspx/kernel32.AttachConsole,) – drzaus Jan 11 '18 at 18:12
-
1
It´s very simple to do:
Just add following attribute and code to your Main-method:
[STAThread]
void Main(string[] args])
{
Application.EnableVisualStyles();
//Do some stuff...
while(!Exit)
{
Application.DoEvents(); //Now if you call "form.Show()" your form won´t be frozen
//Do your stuff
}
}
Now you´re fully able to show WinForms :)

- 1,761
- 2
- 32
- 45
-
1Works fine! If you need to check for keystrokes, you'll have to check for key availability using Console.KeyAvailable before calling Console.ReadKey, otherwise you'll be blocking the event loop (and thus your forms) – wexman May 06 '15 at 09:46
You can create a winform project in VS2005/ VS2008 and then change its properties to be a command line application. It can then be started from the command line, but will still open a winform.

- 42,717
- 16
- 86
- 131
All the above answers are great help, but I thought to add some more tips for the absolute beginner.
So, you want to do something with Windows Forms, in a Console Application:
Add a reference to System.Windows.Forms.dll in your Console application project in Solution Explorer. (Right Click on Solution-name->add->Reference...)
Specify the name space in code: using System.Windows.Forms;
Declare the needed properties in your class for the controls you wish to add to the form.
e.g. int Left { get; set; } // need to specify the LEFT position of the button on the Form
And then add the following code snippet in Main()
:
static void Main(string[] args)
{
Application.EnableVisualStyles();
Form frm = new Form(); // create aForm object
Button btn = new Button()
{
Left = 120,
Width = 130,
Height = 30,
Top = 150,
Text = "Biju Joseph, Redmond, WA"
};
//… more code
frm.Controls.Add(btn); // add button to the Form
// …. add more code here as needed
frm.ShowDialog(); // a modal dialog
}

- 27,532
- 16
- 147
- 165

- 89
- 3
This worked for my needs...
Task mytask = Task.Run(() =>
{
MyForm form = new MyForm();
form.ShowDialog();
});
This starts the from in a new thread and does not release the thread until the form is closed. Task
is in .Net 4 and later.

- 8,041
- 2
- 47
- 42
You should be able to use the Application class in the same way as Winform apps do. Probably the easiest way to start a new project is to do what Marc suggested: create a new Winform project, and then change it in the options to a console application

- 35,875
- 47
- 158
- 240
Its totally depends upon your choice, that how you are implementing.
a. Attached process , ex: input on form and print on console
b. Independent process, ex: start a timer, don't close even if console exit.
for a,
Application.Run(new Form1());
//or -------------
Form1 f = new Form1();
f.ShowDialog();
for b, Use thread, or task anything, How to open win form independently?

- 1,275
- 15
- 20
If you want to escape from Form Freeze and use editing (like text for a button) use this code
Form form = new Form();
Form.Button.Text = "randomText";
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.Run(form);

- 459
- 1
- 5
- 16