故障排除"程序不包含静态'主'方法",当它显然...?

ade*_*ena 48 c# compilation

我的MS Visual C#程序正在编译并运行得很好.我关闭了MS Visual C#,以便在生活中做其他事情.

我重新打开它(在做任何其他事情之前)去"发布"我的程序并得到以下错误消息:

程序C:\ myprogram.exe不包含适用于入口点的静态"Main"方法

咦?是的它确实......而且它们都提前15分钟工作了.当然,我可以相信,在我关闭之前,我不小心碰到了什么或做了什么......但是什么?我该如何解决这个问题?

我的Program.cs文件如下所示:

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

......那里有一些评论.没有其他错误.

救命?

Qui*_*ome 50

文件中的属性是否设置为Compile?

  • 在项目树中选择文件,然后打开属性窗口.最高值应该是正确的. (3认同)
  • 就是这样......我在关闭它之前看了"构建动作",并且必须意外地按键调整我的方式将Program.cs设置为不编译.谢谢! (2认同)

Bas*_*ANI 29

我正在努力解决这个错误只是因为我的一个class library项目 was set acceddentaly是一个控制台应用程序

因此,请确保您的类库项目是输出类型中的类库

在此输入图像描述


小智 20

哦,我也在看这个问题.在我看来,解决方案太简单了.我在解决方案中添加了一个新的空项目.新添加的项目将自动设置为控制台应用程序.但由于添加的项目是一个"空"项目,因此该新项目中不存在任何Program.cs.(正如所料)

我需要做的就是将项目属性的输出类型更改为类库

  • 谢谢!那是我的问题:) (3认同)

小智 13

如果您有WPF或Silverlight应用程序,请确保App.xaml具有"ApplicationDefinition"作为文件属性上的BuildAction.


8pr*_*ons 9

我已经改变了

public static void Main(string[] args)
Run Code Online (Sandbox Code Playgroud)

public async static void Main(string[] args)
Run Code Online (Sandbox Code Playgroud)

问题是它需要是一个任务:

public async static Task Main(string[] args)
Run Code Online (Sandbox Code Playgroud)


Ali*_*adi 7

我有这个错误并通过这个解决方案解决了。

--> Right click on the project

--> and select "Properties"

--> then set "Output Type" to "Class Library".
Run Code Online (Sandbox Code Playgroud)


Iga*_*nik 5

检查项目的属性.在"应用程序"选项卡上,选择您的Program类作为启动对象:

替代文字

  • 如果那个下拉菜单是空的怎么办? (5认同)
  • 只有在有两个静态 Main 方法时才需要设置。 (2认同)