不包含适合于入口点的静态“ main”方法

我开始将每天的代码组织成独立的代码。Cs 文件,并且为了允许与 UI 一起工作的方法继续这样做,我将创建。Cs 代码使用相同的命名空间和公共分部类名,因此这些方法可以互操作。

我的头在四个文件中看起来是这样的,包括我的主核心文件,它调用:

public shell()
{
InitializeComponent();
}

使用 UI 的.cs 文件的头部区域(似乎正在导致这种新的冲突) :

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using System.Net;
using System.Runtime.InteropServices;
using watin = WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using System.Web;




namespace WindowsFormsApplication1
{


public partial class shell : Form
{

现在,当我尝试调试/预览我的应用程序时(顺便说一下,这是 Visual Studio 2010 Express 中的一个 Windows 应用程序) ,我得到了这个错误消息:

不包含适合于入口点的静态“ main”方法

我在 Application-> Startup 对象中查看了应用程序属性,但它没有提供任何选项。如何通知应用程序从。Cs 文件,该文件包含我的 InitializeComponent () ; 命令?

  • 我到现在都没找到解决办法。
  • 每个.cs 文件上的属性都设置为“ Compile”。
  • 我在解决方案资源管理器中没有看到 App.xaml 文件,但是我看到了 app.config 文件。

我还是个新手,这是我第一次尝试用 c # 代码组织方法。

352373 次浏览

If you do indeed have a public static main method it could be your build settings as explained in this question: Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?

Looks like a Windows Forms project that is trying to use a startup form but for some reason the project properties is set to startup being Main.

If you have enabled application framework you may not be able to see that Main is active (this is an invalid configuration).

I was looking at this issue as well, and in my case the solution 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

Try adding this method to a class and see if you still get the error:

[STAThread]
static void Main()
{
}

hey i got same error and the solution to this error is just write Capital M instead of small m.. eg:- static void Main() I hope it helps..

If you don't have a file named Program.cs, just add a new Class and name it Program.cs.

Then paste this code:

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


namespace Sales {
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());
}
}


}

Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.

If you do have a Main method but still get this error, make sure that the file containing the Main method has "Build action" set to "Compile" and "Copy to ouput directory" set to "Do not copy".

I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.

  1. Select App.xaml and display its properties. Set Build Action to ApplicationDefinition.
  2. App.xaml and its corresponding *.cs file must be placed into the root directory of the *.csproj file, i. e. not into a "Source" folder.

I am using Visual Studio and also had this problem. It took me some time, but in my program it was caused because I accidentally deleted a Class named "Program" that is generated automatically.

For future readers who faced same issue with Windows Forms Application, one solution is to add these lines to your main/start up form class:

    [STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyMainForm());
}

Then go to project properties > Application > Startup Object dropdown, should see the namespace.MyMainForm, select it, clean and build the solution. And it should work.

Check to see if the project is set as the "Startup Project"

Right click on the project and choose "Set as Startup Project" from the menu.

When you want to allow paramaters to be specified from the command, they must look like this:

 [STAThread]
static void Main(params string[] paramaters)
{

you cannot specify more than one paramater, otherwise this will also cause the error reported above.

For some others coming here:

In my case I had copied a .csproj from a sample project which included <EnableDefaultCompileItems>false</EnableDefaultCompileItems> without including the Program.cs file. Fix was to either remove EnableDefaultCompileItems or include Program.cs in the compile explicitly

If you are like me, then you might have started with a Class Library, and then switched this to a Console Application. If so, change this...

namespace ClassLibrary1
{
public class Class1
{
}
}

To this...

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}

hellow your main class was deleted so add new class that name set as Main.cs and pest that code or if porblem in window so same problem on that

using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;


namespace your_PKG_name.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");


}
}
}

A valid entry looks like:

public static class ConsoleProgram
{
[STAThread]
static void Main()
{
Console.WriteLine("Got here");
Console.ReadLine();
}
}

I had issues as I'm writing a web application, but for the dreadly loading time, I wanted to quickly convert the same project to a console application and perform quick method tests without loading the entire solution.

My entry point was placed in /App_Code/Main.cs, and I had to do the following:

  1. Set Project -> Properties -> Application -> Output type = Console Application
  2. Create the /App_Code/Main.cs
  3. Add the code above in it (and reference the methods in my project)
  4. Right click on the Main.cs file -> Properties -> Build Action = Compile

After this, I can set the output (as mentioned in Step 1) to Class Library to start the web site, or Console Application to enter the console mode.

Why I did this instead of 2 separate projects?

Simply because I had references to Entity Framework and other specific references that created problems running 2 separate projects.

For easier solutions, I would still recommend 2 separate projects as the console output is mainly test code and you probably don't want to risk that going out in production code.

If you are using a class library project then set Class Library as output type in properties under application section of project.

I had this error and solved it using this solution.

  1. Right click on the project
  2. Select "Properties"
  3. Set "Output Type" to "Class Library".

Salaam, I have both Visual Studio 2017 and Visual Studio 2019

Visual Studio 2019 does not show this error but 2017 does. Try Installing Visual Studio 2019.


Visual Studio 2017

Visual Studio 2017


Visual Studio 2019

Visual Studio 2019

For me, the error was actually produced by "Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater". This issue was resulting in the "Does not contain a static 'main' method suitable for an entry point" message in the Error List, but the Output window showed the "not available" error. To correct this, I changed the language version from 'C# latest minor version (default)' to 'C# latest minor version (latest)' under Advanced Build Settings.

Edit .csproj file

<OutputType>Library</OutputType>

cheers !

Had this problem in VS 2017 caused by:

static async Task Main(string[] args)

(Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater)

Adding

<LangVersion>latest</LangVersion>

to app.csproj helped.

After placing the above code in Program.cs, follow below steps

  1. Right click on the project

  2. Select Properties

  3. Set Output Type to Windows Application

  4. Startup object : namepace.Program

Perhaps unintentional, but moving my docker file to the solution folder instead of the project eliminated the error. This was helpful when I still wanted to run the solution independently of docker

enter image description here

Just right click on project and select properties and then set Output type on Class Library

Another situation where this occur is when someone (unintentionally) changes Build Action for Program.cs. The value for Build Action should be C# compiler.

I accidentally changed Build Action to None, which removed program.cs from the project and therefore wasn't included when compile started.

If you use Visual Studio Code change Project Sdk="Microsoft.NET.Sdk.Web" to Project Sdk="Microsoft.NET.Sdk" on csproj file.

Did you accidentally remove the entire Program.cs file? If you have removed,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListWievKullanımı
{
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());
}
}
}

This might work for you.

enter image description here

Use the following -

async static **Task** Main(string[] args)

I got this error when using the command Build Docker Image in Visual Studio 2022.

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

The project built perfectly well in Windows but I tried to build a Linuxcontainer. Switching to Output Type Class Library solved the error but Docker Compose gave me this error instead:

CTC1031 Linux containers are not supported for

https://stackoverflow.com/a/74044317/3850405

I tried explicitly using a Main method like this but it did not work:

namespace WebApplication
{
public class Program
{
public static void Main(string[] args)
{

I have no idea why but this solved it for me:

Gives error:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443


FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj", "Services/Classification/ClassificationService.Api/"]


RUN dotnet restore "Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj"
COPY . .
WORKDIR "/src/Services/Classification/ClassificationService.Api"
RUN dotnet build "ClassificationService.Api.csproj" -c Release -o /app/build

Works:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443


FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj", "src/Services/Classification/ClassificationService.Api/"]


RUN dotnet restore "src/Services/Classification/ClassificationService.Api/ClassificationService.Api.csproj"
COPY . .
WORKDIR "/src/src/Services/Classification/ClassificationService.Api"
RUN dotnet build "ClassificationService.Api.csproj" -c Release -o /app/build

Notice the double /src in the working example.

I read that you had to place the Dockerfile at the same level as .sln file but in my case the files are separated by four levels.

https://stackoverflow.com/a/63257667/3850405

Add

static async Task Main(string[] args)
{
}

instead of

static async void Main(string[] args)
{
}

its work for me.