63

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?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
adeena
  • 4,027
  • 15
  • 40
  • 52

20 Answers20

62

Are the properties on the file set to Compile?

Quibblesome
  • 25,225
  • 10
  • 61
  • 100
  • 3
    That was it... I was looking at "build Actions" before I closed it and must have accidentally key-stroked my way to set the Program.cs to not compile. Thanks! – adeena Dec 22 '08 at 18:03
  • 1
    How can I check this? I'm having the same problem here... PS: Using vs2012 – Michel Ayres Feb 06 '13 at 18:22
  • 3
    Select the file in the project tree and open the properties window. The top value should be right one. – Quibblesome Feb 06 '13 at 18:24
60

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

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
22

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

Freek Bos
  • 267
  • 2
  • 5
17

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
MrOnigiri
  • 191
  • 1
  • 4
9

I had this error and solved by this solution.

--> Right click on the project

--> and select "Properties"

--> then set "Output Type" to "Class Library".
Ali Ahmadi
  • 563
  • 5
  • 9
6

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

alt text

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
5

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)
8protons
  • 3,591
  • 5
  • 32
  • 67
3

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?

configurator
  • 40,828
  • 14
  • 81
  • 115
  • 1
    Yes - it was failing on every compile and yes - I did accidentally change my build action on Program.cs to "none" :) – adeena Dec 22 '08 at 18:05
  • 2
    How can a 20k+ rep user post a "comment" as answer, and still get upvotes? Or does this actually somehow answer the question and I just didn't notice it? – Alisson Reinaldo Silva Nov 30 '17 at 18:21
  • Nevermind, this is 2008 (I thought it was recent because it was in LQ queue, my bad). – Alisson Reinaldo Silva Nov 30 '17 at 18:27
  • @Alisson firstly, yes, the answer was from 2008, but the second paragraph also answered the question. – configurator Dec 10 '17 at 15:52
  • @configurator yep, my bad, I just didn't notice. I think you should have edited your answer to look more like an actual answer, and it should be the accepted one (since you were the first to post). But these things happen, unfortunately. – Alisson Reinaldo Silva Dec 11 '17 at 01:29
3

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>
PontiusTheBarbarian
  • 1,004
  • 9
  • 17
2

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.

Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64
1

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.

satyajit
  • 1,470
  • 3
  • 22
  • 43
1

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

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

James Hirschorn
  • 7,032
  • 5
  • 45
  • 53
0

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

0

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.

  • 3
    the `async Task` was also the problem for me - because I was targeting the wrong version of C#. I added this to my csproj and it all started working: `7.1true` – Tom Carver Apr 05 '19 at 13:05
0

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

HHansen
  • 366
  • 3
  • 3
0

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

joym8
  • 4,014
  • 3
  • 50
  • 93
0

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>
0

In my case i had this error in some of my tests projects (Application.Tests, Domain.Tests, WebApp.Tests) simply because the versions of nugget packages for 'Microsoft.NET.Test.Sdk' installed in these projects were different. So what i did was to make the versions of the nugget packages for "Microsoft.NET.Test.Sdk" are the same.

Riley Christian
  • 149
  • 1
  • 13
0

Main should be spelled with an uppercase M. I had mine in lower case, c++ style.

CppDev
  • 49
  • 5
  • While this is true, for applications that are not using top-level statements or for versions of c# earlier than 9, this question was already marked answered for a completely different reason. – dodexahedron Jul 20 '23 at 03:00