[c#] Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?

My MS Visual C# program was compiling and running just fine. I close MS Visual C# to go off and do other things in life.

I reopen it and (before doing anything else) go to "Publish" my program and get the following error message:

Program C:\myprogram.exe does not contain a static 'Main' method suitable for an entry point

Huh? Yes it does... and it was all working 15 min earlier. Sure, I can believe that I accidentally hit something or done something before I closed it up... but what? How do I troubleshoot this?

My Program.cs file looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace SimpleAIMLEditor
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new mainSAEForm());
        }
    }
}

...and there are some comments in there. There are no other errors.

Help?

This question is related to c# compilation

The answer is


It is important that the Main method is placed in the class that is properly configured in Visual Studio as a start-up object:

  • Right-click your project in project explorer; choose "Properties..."
  • In the properties dialog, choose "Application" tab
  • In the application tab, choose SimpleAIMLEditor.Program from the drop-down for your start-up object

Now run your application again. The error will disappear.


That's odd. Does your program compile and run successfully and only fail on 'Publish' or does it fail on every compile now?

Also, have you perhaps changed the file's properties' Build Action to something other than Compile?


In my case (where none of the proposed solutions fit), the problem was I used async/await where the signature for main method looked this way:

static async void Main(string[] args)

I simply removed async so the main method looked this way:

static void Main(string[] args)

I also removed all instances of await and used .Result for async calls, so my console application could compile happily.


Oke, I was looking at this issue as well. And in my case the solutions was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)

All I needed to do was change the output type of the project properties to Class library


Check your project's properties. On the "Application" tab, select your Program class as the Startup object:

alt text


For me same error occurred because I renamed the namespace of the Program class.
The "Startup object" field in "Application" tab of the project still referenced the old namespace. Selecting new startup object solved the problem and also removed the obsolete entry from that list.
You may also want to update "Default namespace" field in the same "Application" tab.


What I found is that the Program.cs file was not part of the solution. I did an add existing item and added the file (Program.cs) back to the solution.

This corrected the error: Error 1 Program '..... does not contain a static 'Main' method suitable for an entry point


My problem is that I accidentally set the arguments for Main

static void Main(object value)

thanks to my refactoring tool. Took couple mins to figure out but should help someone along the way.


If you have a WPF or Silverlight application, make sure that App.xaml has "ApplicationDefinition" as the BuildAction on the File Properties.


What worked for me: Close MS Visual Studio, then start Visual Studio and open the solution. The error message was then gone.


I was struggle with this error just because one of my class library projects was set acceddentaly to be an console application

so make sure your class library projects is class library in Output type

enter image description here


Check your project's properties. On the "Application" tab, select your Program class as the Startup object:

alt text


In my case (where none of the proposed solutions fit), the problem was I used async/await where the signature for main method looked this way:

static async void Main(string[] args)

I simply removed async so the main method looked this way:

static void Main(string[] args)

I also removed all instances of await and used .Result for async calls, so my console application could compile happily.


I had this error and solved by this solution.

--> Right click on the project

--> and select "Properties"

--> then set "Output Type" to "Class Library".

It is important that the Main method is placed in the class that is properly configured in Visual Studio as a start-up object:

  • Right-click your project in project explorer; choose "Properties..."
  • In the properties dialog, choose "Application" tab
  • In the application tab, choose SimpleAIMLEditor.Program from the drop-down for your start-up object

Now run your application again. The error will disappear.


Check your project's properties. On the "Application" tab, select your Program class as the Startup object:

alt text


I had this problem and its because I wasnt using the latest version of C# (C# 7.3 as of writing this) and I had to change the below internal access modifier to public.

internal class Program
 {
    public static void Main(string[] args)
    {
        //My Code
    }
  }

or ammend the .csproj to use the latest version of C# (this way meant I could still keep the above class as internal):

<PropertyGroup>
    <LangVersion>latest</LangVersion>
    <NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>

For me same error occurred because I renamed the namespace of the Program class.
The "Startup object" field in "Application" tab of the project still referenced the old namespace. Selecting new startup object solved the problem and also removed the obsolete entry from that list.
You may also want to update "Default namespace" field in the same "Application" tab.


Check your project's properties. On the "Application" tab, select your Program class as the Startup object:

alt text


Oke, I was looking at this issue as well. And in my case the solutions was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)

All I needed to do was change the output type of the project properties to Class library


I was struggle with this error just because one of my class library projects was set acceddentaly to be an console application

so make sure your class library projects is class library in Output type

enter image description here


If you have a WPF or Silverlight application, make sure that App.xaml has "ApplicationDefinition" as the BuildAction on the File Properties.


That's odd. Does your program compile and run successfully and only fail on 'Publish' or does it fail on every compile now?

Also, have you perhaps changed the file's properties' Build Action to something other than Compile?


My problem is that I accidentally set the arguments for Main

static void Main(object value)

thanks to my refactoring tool. Took couple mins to figure out but should help someone along the way.


What worked for me: Close MS Visual Studio, then start Visual Studio and open the solution. The error message was then gone.


What I found is that the Program.cs file was not part of the solution. I did an add existing item and added the file (Program.cs) back to the solution.

This corrected the error: Error 1 Program '..... does not contain a static 'Main' method suitable for an entry point


I had this problem and its because I wasnt using the latest version of C# (C# 7.3 as of writing this) and I had to change the below internal access modifier to public.

internal class Program
 {
    public static void Main(string[] args)
    {
        //My Code
    }
  }

or ammend the .csproj to use the latest version of C# (this way meant I could still keep the above class as internal):

<PropertyGroup>
    <LangVersion>latest</LangVersion>
    <NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>

I had this error and solved by this solution.

--> Right click on the project

--> and select "Properties"

--> then set "Output Type" to "Class Library".

That's odd. Does your program compile and run successfully and only fail on 'Publish' or does it fail on every compile now?

Also, have you perhaps changed the file's properties' Build Action to something other than Compile?