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

Viewed 157771

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?

18 Answers

Are the properties on the file set to Compile?

I had this error and solved by this solution.

--> Right click on the project

--> and select "Properties"

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

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

alt text

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?

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 changed

public static void Main(string[] args)

to

public async static void Main(string[] args)

The problem is that it needs to be a task:

public async static Task Main(string[] args)

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

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.

In my case it was an XUnit test library showing this error. I had mistakenly deleted the Microsoft.NET.Test.Sdk package.

In my dotnet core 3.1 project I had created a new class/file instead of Program.cs that contained the Main method.

I had to update the Startup object to my new class name in Project Properties

enter image description here

I had a similar problem but what solved it for me was making sure my launch.json and .csproj file were pointing to the same framework.

In launch.json this

"program": "${workspaceFolder}/bin/Debug/net6.0-windows/myproject.dll"

Was not the same as this in .csproj

<TargetFramework>net6.0-windows</TargetFramework>
Related