修复“包含重复的‘编译’项目".Visual Studio 中的错误 [英] Fixing the "Duplicate 'Compile' items were included." error in Visual Studio

查看:24
本文介绍了修复“包含重复的‘编译’项目".Visual Studio 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常烦人的 VS 错误,我使用的是 VS 2019 16.8.1

This is a very annoying VS error, I'm on VS 2019 16.8.1

我花了很多时间修复它:

I spend a lot of time fixing it:

包含重复的编译"项目..NET SDK 包括默认情况下,从项目目录中编译"项目.你可以从您的项目文件中删除这些项目,或设置如果您想将EnableDefaultCompileItems"属性设置为false"明确地将它们包含在您的项目文件中.想要查询更多的信息,请参阅 https://aka.ms/sdkimplicititems.重复的项目是:'Program.cs' Service.HostFilesdotnetsdk5.0.101SdksMicrosoft.NET.Sdk argetsMicrosoft.NET.Sdk.DefaultItems.targets 295

Duplicate 'Compile' items were included. The .NET SDK includes 'Compile' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultCompileItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'Program.cs' Service.Host Filesdotnetsdk5.0.101SdksMicrosoft.NET.Sdk argetsMicrosoft.NET.Sdk.DefaultItems.targets 295

我不明白是什么导致了这个错误.我注释掉了 *.csproj 文件中的 Include 标签,但问题是我有 30 多个项目,突然之间它们都有相同的重复错误,遍历所有修复这样一个神秘的错误似乎是不安全的行动,其中一些项目是很久没有接触过的遗留项目.注释 Include 标记的解决方案中的另一个问题是,我有一些具有此包含结构的项目:

I can't understand what causes this error. I commented out the Include tags in the *.csproj files, but the problem is that I have more than 30 projects, all of a sudden they all have the same duplicate error, going through all of them to fix such a mysterious error seems unsafe action, some of these projects are legacy projects that haven't been touched for a long time. Another problem in the solution commenting the Include tags is that I have some project with this include structure:

  <ItemGroup>
    <Compile Include="Contracts.cs" />
    <Compile Include="IKey.cs">
      <DependentUpon>Contracts.cs</DependentUpon>
    </Compile>
    <Compile Include="ISigner.cs">
      <DependentUpon>Contracts.cs</DependentUpon>
    </Compile>
  </ItemGroup>

评论这个会影响项目结构.

commenting this will affect the project structure.

我试图注释掉文件中的 CheckForDuplicateItems 标签:

I tried to comment out the CheckForDuplicateItems tags in the file:

C:Program Filesdotnetsdk5.0.101SdksMicrosoft.NET.Sdk argetsMicrosoft.NET.Sdk.DefaultItems.targets",

"C:Program Filesdotnetsdk5.0.101SdksMicrosoft.NET.Sdk argetsMicrosoft.NET.Sdk.DefaultItems.targets",

错误消失了.

编辑

我刚刚注意到目标文件路径中包含 .NET 5 版本 (..sdk5.0.101..),我只有一个带有 .NET 5 的项目,这是否会导致问题?

I just noticed that the .NET 5 version (..sdk5.0.101..) is included in the targets file path, I only have one project with .NET 5, does this cause the problem?

推荐答案

.NET5 使用新样式的项目,这些项目会自动将许多文件包含到您的项目中.这有助于保持项目文件较小,并且不会导致每次添加、删除或重命名文件时都检查和更改它们.一次为所有项目解决此问题的最简单方法可能是在项目的根文件夹中添加一个 directory.build.props 以设置 false 一次用于所有项目.

.NET5 uses the new style projects which auto-include a number of files into your project. This helps keep the projects files small and doesn't cause them to be checked out and changed every time you add, remove or rename a file. Probably the easiest way to fix this for all projects at once is to add a directory.build.props in the root folder of your project to set the <EnableDefaultCompileItems>false<EnableDefaultCompileItems> for all projects at once.

<Project>
    <PropertyGroup>
         <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
    </PropertyGroup>
</Project>

不要编辑 sdk 目录中的任何文件,它们是当前和未来所有项目的默认设置.相反,您可以将这些设置添加到您的项目文件或这些解决方案全局文件的目录中,以便在特定文件夹或解决方案中为您的项目更改它们.

Do not edit any files in the sdk directory, they are the default settings for all projects current and future. Instead you can add these settings to your project files or these directory of solution global files to change them for your projects in a specific folder or solution.

见:

这篇关于修复“包含重复的‘编译’项目".Visual Studio 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆