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

127

今天我开始将我的代码组织成单独的.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 object,但它没有提供任何选项。如何告知应用程序从具有我的InitializeComponent();命令的.cs文件开始?

  • 我已经搜索了很久,但还没有找到解决方案。
  • 每个.cs文件上的属性都设置为“编译”。
  • 我在我的解决方案资源管理器中没有看到App.xaml文件,但我确实看到一个app.config文件。

我还是很新手,这是我尝试使用C#代码进行组织的第一次尝试。


13
你有一个主方法吗? - bobek
1
你需要一个名为main的静态方法,并且签名正确。这样编译器就知道如何启动你的程序。 - David Heffernan
有没有办法我可以手动编程解决方案从哪里开始?我很困惑为什么它找不到静态的“Main”方法,因为我似乎找不到任何指示代码告诉它查找“Main”方法。我对c#非常陌生。 --- [编辑] - @David Hefferman,您能详细说明“正确签名”的含义吗? - atwellpub
6
尝试将类似以下代码段添加到您的项目中:[STAThread] static void Main(string[] args) { Application.Run(new shell()); } 这样做可以确保在单线程单元 (STA) 中运行应用程序,并启动名为shell的新窗体。 - L.B
1
@L.B:将其创建为答案,这样他就可以接受它。 - Joshua
显示剩余3条评论
33个回答

1

对于其他人来说:

在我的情况下,我从一个示例项目中复制了一个 .csproj 文件,其中包含 <EnableDefaultCompileItems>false</EnableDefaultCompileItems>,但没有包括 Program.cs 文件。解决方法是要么删除 EnableDefaultCompileItems,要么显式地将 Program.cs 包含在编译中。


1
另一个出现这种情况的情形是当有人(无意中)更改了Program.cs文件的构建操作(Build Action)构建操作(Build Action)的值应为C#编译器(C# compiler)
我不小心将构建操作(Build Action)更改为无(None),这会将program.cs从项目中移除,因此在编译开始时未包含该文件。

1

您是否意外删除了整个Program.cs文件?如果您已经删除,

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

这可能适合你。

在此输入图像描述


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

1
我在使用 Visual Studio 2022 的“构建 Docker 镜像”命令时遇到了这个错误。
error CS5001: Program does not contain a static 'Main' method suitable for an entry point

该项目在Windows上构建得非常完美,但我尝试构建Linux容器。将“输出类型”切换为“类库”解决了错误,但Docker Compose给了我以下错误提示:

CTC1031:不支持Linux容器。

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

我尝试明确地使用类似于这样的Main方法,但它没有起作用:
namespace WebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {

我不知道为什么,但这对我解决了它。
显示错误:
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

作品:
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

请注意示例中的双重`/src`。
我看到你必须将Dockerfile放在与.sln文件相同的级别,但在我的情况下,这些文件被分为四个级别。

https://dev59.com/0lQJ5IYBdhLWcg3w2Ji8#63257667


不知道为什么,但这解决了我的问题。 - Dany

0

对于未来遇到 Windows Forms 应用程序相同问题的读者,一个解决方案是将以下行添加到您的主/启动窗体类中:

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

然后进入项目属性 > 应用程序 > 启动对象下拉菜单,应该会看到 namespace.MyMainForm,选择它,清理并构建解决方案。这样就可以了。


0

0

检查项目是否设置为“启动项目”

右键单击项目,从菜单中选择“设置为启动项目”。


0

我正在使用Visual Studio,也遇到了这个问题。花了一些时间,但在我的程序中,原因是我不小心删除了一个名为“Program”的自动生成的类。


0

我也曾经遇到过这个问题。后来我意识到我选择的是控制台应用程序(包)而不是控制台应用程序。


0
如果您使用Visual Studio Code,请在csproj文件中将Project Sdk="Microsoft.NET.Sdk.Web"更改为Project Sdk="Microsoft.NET.Sdk"。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接