Application name does not exist in current context

I have an existing winform application that did not use Program.cs. I wanted to add Program.cs with regular code

static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } 

but i have an error message

The application does not exist in the current context.

How to solve this?

+4
source share
5 answers

At the beginning of the file should be a using directive for System.Windows.Forms :

 using System.Windows.Forms; static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } 

Either this, or change the Main method to use full type names:

 System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); System.Windows.Forms.Application.Run(new Form1()); 

... although I would recommend the first solution.

+10
source

Make sure you add a link to the System.Windows.Forms assembly and add a link to the System.Windows.Forms .

+2
source

If you use VS, use Alt + Shift + F10, it will help you or install resharper

+2
source

The application class lives in the System.Windows.Forms namespace. You need to add a link at the top of this file or explicitly evaluate the path.

+1
source

Does the file Program.cs using System.Windows.Forms; ? The Application class is in this namespace.

+1
source