C# 对“程序不包含静态‘Main’方法”进行故障排除,当它清楚地...?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/386831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:29:52  来源:igfitidea点击:

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

c#compilation

提问by adeena

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.

我的 MS Visual C# 程序编译运行得很好。我关闭 MS Visual C# 去做其他事情。

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

程序 C:\myprogram.exe 不包含适合入口点的静态“Main”方法

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?

嗯?是的,它确实......而且它在 15 分钟前就开始工作了。当然,我可以相信我在关闭之前不小心撞到了什么东西或做了什么......但是什么?我该如何解决这个问题?

My Program.cs file looks like this:

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

...and there are some comments in there. There are no other errors.

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

Help?

帮助?

采纳答案by Quibblesome

Are the properties on the file set to Compile?

文件的属性是否设置为编译?

回答by configurator

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 Actionto something other than Compile?

此外,你也许改变文件属性Build Action比其他的东西Compile

回答by Igal Tabachnik

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

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

alt text

替代文字

回答by Igal Tabachnik

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.

我发现 Program.cs 文件不是解决方案的一部分。我添加了现有项目并将文件 (Program.cs) 添加回解决方案。

This corrected the error: Error 1 Program '..... does not contain a static 'Main' method suitable for an entry point

这更正了错误:错误 1 ​​程序 '..... 不包含适合入口点的静态 'Main' 方法

回答by MrOnigiri

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

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

回答by Freek Bos

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)

好的,我也在看这个问题。就我而言,解决方案太简单了。我在解决方案中添加了一个新的空项目。新添加的项目会自动设置为控制台应用程序。但由于添加的项目是“空”项目,因此该新项目中不存在 Program.cs。(如预期)

All I needed to do was change the output type of the project properties to Class library

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

回答by satyajit

My problem is that I accidentally set the arguments for Main

我的问题是我不小心设置了 Main 的参数

static void Main(object value)

static void Main(对象值)

thanks to my refactoring tool. Took couple mins to figure out but should help someone along the way.

感谢我的重构工具。花了几分钟才弄明白,但应该帮助别人。

回答by Roland Pihlakas

For me same error occurred because I renamed the namespaceof the Program class.
The "Startup object"field in "Application" tabof 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.

对我来说,发生了同样的错误,因为我重命名了 Program 类的命名空间
“启动对象”在现场“应用程序”选项卡中的项目仍引用旧的命名空间。选择新的启动对象解决了问题,并从该列表中删除了过时的条目。
您可能还想更新同一“应用程序”选项卡中的“默认命名空间”字段。

回答by Basheer AL-MOMANI

I was struggle with this error just because one of my class libraryprojectswas set acceddentalyto be an console application

我一直在为这个错误而苦苦挣扎,因为我的一个class library项目was set acceddentaly是一个控制台应用程序

so make sure your class library projects is class library in Output type

所以确保你的类库项目是输出类型的类库

enter image description here

在此处输入图片说明

回答by dasblinkenlight

It is important that the Mainmethod is placed in the class that is properly configured in Visual Studio as a start-up object:

重要的是将该Main方法放置在在 Visual Studio 中正确配置为启动对象的类中:

  • Right-click your project in project explorer; choose "Properties..."
  • In the properties dialog, choose "Application" tab
  • In the application tab, choose SimpleAIMLEditor.Programfrom the drop-down for your start-up object
  • 在项目资源管理器中右键单击您的项目;选择“属性...”
  • 在属性对话框中,选择“应用程序”选项卡
  • 在应用程序选项卡中,SimpleAIMLEditor.Program从您的启动对象的下拉列表中选择

Now run your application again. The error will disappear.

现在再次运行您的应用程序。错误将消失。