C# 向我的项目添加了一个新类并收到错误消息,说 Program.Main() 有多个条目,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11747761/
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-09 19:05:12  来源:igfitidea点击:

Added a new class to my project and get error say Program.Main() has more than one entry why?

c#

提问by user1544479

The problem is that after i added the new class the error came up when i did build the solution. What can be wrong ?

问题是,在我添加新类之后,当我构建解决方案时出现了错误。有什么问题?

In Form1 i dont have any code yet.

在 Form1 中,我还没有任何代码。

Just added new class:

刚刚添加了新类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;

namespace OpenHardwareMonitorReport
{

    class Program
    {

        static void Main(string[] args)
        {
            Computer computer = new Computer();
            computer.Open();

            var temps = new List<decimal>();
            foreach (var hardware in computer.Hardware)
            {
                if (hardware.HardwareType != HardwareType.CPU)
                    continue;
                hardware.Update();
                foreach (var sensor in hardware.Sensors)
                {
                    if (sensor.SensorType != SensorType.Temperature)
                    {
                        if (sensor.Value != null)
                            temps.Add((decimal)sensor.Value);
                    }
                }
            }

            foreach (decimal temp in temps)
            {
                Console.WriteLine(temp);
            }
            Console.ReadLine();
        }
    }
}

Then i see Program.cs and error on Main()

然后我在 Main() 上看到 Program.cs 和错误

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

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

Error   2   Program 'D:\C-Sharp\NvidiaTemp\NvidiaTemp\NvidiaTemp\obj\x86\Debug\NvidiaTemp.exe' has more than one entry point defined: 'NvidiaTemp.Program.Main()'.  Compile with /main to specify the type that contains the entry point.   D:\C-Sharp\NvidiaTemp\NvidiaTemp\NvidiaTemp\Program.cs  14  21  NvidiaTemp

采纳答案by DGH

A C# program can only have one Program.Main(). Main is the first method run when the program starts, so the compiler needs to know which one is the real one, and it can't if you have two.

AC# 程序只能有一个 Program.Main()。Main 是程序启动时运行的第一个方法,所以编译器需要知道哪一个是真正的,如果你有两个就不能。

It looks like you're making a Windows application. You should either add code to the existing main, or add it to an event handler triggered by your main form.

看起来您正在制作 Windows 应用程序。您应该将代码添加到现有的 main 中,或者将其添加到由主窗体触发的事件处理程序中。

回答by Oded

A .NET program should have only onestatic Mainmethod.

.NET 程序应该只有一个静态Main方法。

You have two, and the compiler doesn't know which one to use.

你有两个,编译器不知道使用哪一个。

Rename the pasted one, unless you want itto be the entry point to the application (in which case, rename the other), or compile the application passing using the /mainswitch specifying whichof the Mainmethods to use.

重命名粘贴一个,除非你想是入口点的应用程序(在该情况下,重命名等),或编译使用传递应用/main开关指定其中Main使用方法。

See Main() and Command-Line Arguments (C# Programming Guide)on MSDN for more detail:

有关更多详细信息,请参阅MSDN 上的Main() 和命令行参数(C# 编程指南)

The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.

There can only be one entry point in a C# program.If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point. For more information, see /main (C# Compiler Options).

Main 方法是 C# 控制台应用程序或 Windows 应用程序的入口点。(库和服务不需要 Main 方法作为入口点。)。当应用程序启动时,Main 方法是第一个被调用的方法。

C# 程序中只能有一个入口点。如果您有多个具有 Main 方法的类,则必须使用 /main 编译器选项编译您的程序,以指定使用哪个 Main 方法作为入口点。有关详细信息,请参阅 /main(C# 编译器选项)。

(emphasis mine)

(强调我的)

回答by Habib

You have two Main methods, that is why you are getting this error.

您有两个 Main 方法,这就是您收到此错误的原因。

From MSDN - Main Method

来自MSDN - 主要方法

There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point.

C# 程序中只能有一个入口点。如果您有多个具有 Main 方法的类,则必须使用 /main 编译器选项编译您的程序,以指定使用哪个 Main 方法作为入口点。

回答by Paul Smith

Others have pointed out that you have two static void Main methods. There are two easy fixes for this, one obvious and one that hasn't been specifically mentioned yet:

其他人指出您有两个静态 void Main 方法。对此有两种简单的修复方法,一种是显而易见的,另一种是尚未特别提及的:

  1. Rename one to anything else e.g. Main1, NotMain, etc.
  2. To set the /main compiler option mentioned by @Habib, just right click on the project node in Solution Explorer, select Properties, and in the Application section select the "Startup object" in the dropdown.
  1. 重命名一个别的例如Main1NotMain等等。
  2. 要设置@Habib 提到的/main 编译器选项,只需在解决方案资源管理器中右键单击项目节点,选择属性,然后在应用程序部分的下拉列表中选择“启动对象”。

With solution 2, you can have identical Main(string[] args)signatures in different classes without the compiler whining.

使用解决方案 2,您可以Main(string[] args)在不同的类中拥有相同的签名,而无需编译器抱怨。

回答by Sielu

If you have fixed the error and Visual Studio still gives you error message, it's worth to remove output folders (by default "bin" and "obj") and then rebuild the project. In my case just clicking on "rebuild" did not help.

如果您已修复错误并且 Visual Studio 仍然为您提供错误消息,则值得删除输出文件夹(默认为“bin”和“obj”),然后重新构建项目。就我而言,只是点击“重建”并没有帮助。

回答by Jesse Q

I experienced this issue after adding a Xunit test class to my .NET Core 2.1 project.

在将 Xunit 测试类添加到我的 .NET Core 2.1 项目后,我遇到了这个问题。

The following article gives a detailed explanation of WHY, and provided the answer that worked for me - here.

下面的文章详细解释了为什么,并提供了对我有用的答案 -这里

Basically, the compiler automatically generates a Mainfor the new class. You can provide a directive in your .csproj file to keep this from happening:

基本上,编译器会自动Main为新类生成一个。您可以在 .csproj 文件中提供指令以防止发生这种情况:

<GenerateProgramFile>false</GenerateProgramFile>

<GenerateProgramFile>false</GenerateProgramFile>

Add this to your <PropertyGroup>section and recompile.

将此添加到您的<PropertyGroup>部分并重新编译。

回答by Bill Tarbell

Having two Main methods is just fine. If you receive the error you mentioned then you need only to tell VS which one you'd like to use.

有两个 Main 方法就好了。如果您收到您提到的错误,那么您只需告诉 VS 您想使用哪一个。

  1. Right-click on your project to view the properties.
  2. Go to the Application tab and choose the entry point you desire from the Startup Object dropdown.
  1. 右键单击您的项目以查看属性。
  2. 转到“应用程序”选项卡,然后从“启动对象”下拉列表中选择所需的入口点。

Here's an example where i have two entry points depending on how i want to dev-test the assembly.

这是一个示例,其中我有两个入口点,具体取决于我想如何开发测试程序集。

enter image description here

在此处输入图片说明

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Parth

When you add a new class in your project and also you write the Main method, When you run your code at that time it shows the error like "More than one Main method found" then you just need to 1)Right click on your project name which is show in "Solution Explorer" then 2) go into Application and in the Startup object dropdown menu choose your class which you want to run. :-If you don't find your class name in it then simply just Restart your IDE and again follow the above Steps.

当您在项目中添加一个新类并编写 Main 方法时,当您当时运行代码时,它会显示“找到多个 Main 方法”之类的错误,那么您只需要 1) 右键单击​​您的项目名称显示在“解决方案资源管理器”中,然后 2) 进入应用程序并在启动对象下拉菜单中选择要运行的类。:-如果您没有在其中找到您的类名,那么只需重新启动您的 IDE 并再次按照上述步骤操作。