C# 不包含适合入口点的静态“main”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9607702/
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
Does not contain a static 'main' method suitable for an entry point
提问by atwellpub
I began organizing my code to day into seperarate .cs files, and in order to allow the methods that work with the UI to continue to do so I would create the .cs code under the same namespace and public partial class name so the methods could be inter-operable.
我今天开始将我的代码组织成单独的 .cs 文件,为了允许与 UI 一起工作的方法继续这样做,我将在相同的命名空间和公共部分类名称下创建 .cs 代码,以便方法可以可以互操作。
My header look like this in four files, including my main core file that calls:
我的标题在四个文件中看起来像这样,包括我调用的主要核心文件:
public shell()
{
InitializeComponent();
}
Header area of .cs files that work with the UI (and seem to be causing this new conflict):
与 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
{
Now when I try to debug/preview my application (BTW this is a Windows Application within Visual Studio 2010 Express) I get this error message:
现在,当我尝试调试/预览我的应用程序(顺便说一句,这是 Visual Studio 2010 Express 中的 Windows 应用程序)时,我收到以下错误消息:
Does not contain a static 'main' method suitable for an entry point
不包含适合入口点的静态“main”方法
I looked in the application properties in Application->Startup object, but it offers me no options. How can I inform the application to begin at the .cs file that has my InitializeComponent(); command?
我查看了 Application->Startup 对象中的应用程序属性,但它没有提供任何选项。如何通知应用程序从包含我的 InitializeComponent() 的 .cs 文件开始;命令?
- I've looked around so far without a solution.
- The properties on each .cs file are set to 'Compile'.
- I do not see an App.xaml file in my Solutions explorer but I do see a app.config file.
- 到目前为止,我环顾四周没有解决方案。
- 每个 .cs 文件的属性都设置为“编译”。
- 我在我的解决方案资源管理器中没有看到 App.xaml 文件,但我看到了一个 app.config 文件。
I'm still very new and this is my first attempt at an organizing method with c# code.
我还是个新手,这是我第一次尝试使用 c# 代码组织方法。
回答by A. Wilson
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...?
如果您确实有一个公共静态 main 方法,那么它可能是您的构建设置,如以下问题中所述:疑难解答“程序不包含静态 'Main' 方法”当它清楚地...?
回答by Joshua
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.
看起来像一个试图使用启动窗体的 Windows 窗体项目,但由于某种原因,项目属性设置为启动为 Main。
If you have enabled application framework you may not be able to see that Main is active (this is an invalid configuration).
如果您启用了应用程序框架,您可能无法看到 Main 处于活动状态(这是一个无效的配置)。
回答by Arne
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)
我也在研究这个问题,就我而言,解决方案太简单了。我在解决方案中添加了一个新的空项目。新添加的项目会自动设置为控制台应用程序。但由于添加的项目是“空”项目,因此该新项目中不存在 Program.cs。(如预期)
All I needed to do was change the output type of the project properties to Class library
我需要做的就是将项目属性的输出类型更改为类库
回答by eyossi
Try adding this method to a class and see if you still get the error:
尝试将此方法添加到类中,看看是否仍然出现错误:
[STAThread]
static void Main()
{
}
回答by Chetan Soni
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..
嘿,我遇到了同样的错误,这个错误的解决方案就是写大写 M 而不是小 m.. 例如:- static void Main() 我希望它有帮助..
回答by ROM
If you don't have a file named Program.cs, just add a new Class and name it Program.cs.
如果您没有名为 的文件Program.cs,只需添加一个新类并将其命名为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());
}
}
}
回答by Automationtested
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”.
将 Project > Properties 下的 Output Type 更改为“Class Library”的输出类型。默认情况下,此设置可能已设置为“控制台应用程序”。
回答by simonbs
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".
如果您确实有 Main 方法但仍然出现此错误,请确保包含 Main 方法的文件将“构建操作”设置为“编译”,并将“复制到输出目录”设置为“不复制”。
回答by Subrahmanya Prasad
I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.
我也遇到过这个问题。然后我意识到我选择的是控制台应用程序(包)而不是控制台应用程序。
回答by Roman Volkov
- Select App.xaml and display its properties. Set Build Actionto ApplicationDefinition.
- 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.
- 选择 App.xaml 并显示其属性。将构建操作设置为ApplicationDefinition。
- App.xaml 及其对应的 *.cs 文件必须放在 *.csproj 文件的根目录中,即不能放在“Source”文件夹中。

