C# 在 .csproj 文件中,什么是 <None Include="..."> ?

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

In a .csproj file, what is <None Include="..."> for?

c#csproj

提问by Emmett

How is

怎么

<None Include="C:\foo.bar" />

different from

不同于

<Content Include="C:\foo.bar" />

?

?

采纳答案by adrianbanks

The MSDN article on the build actionproperty explains the differences.

关于构建操作属性的 MSDN 文章解释了这些差异。

None- The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.

Content- The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.

- 该文件不包含在项目输出组中,也不会在构建过程中编译。一个示例是包含文档的文本文件,例如自述文件。

内容- 文件未编译,但包含在内容输出组中。例如,此设置是 .htm 或其他类型的 Web 文件的默认值。

回答by Marc Gravell

One difference is how they get published; "None" items don't get included in a publish, "Content" items do; for example, on the "Application Files" dialog on the Publish tab.

一个区别是它们的发布方式;“无”项目不会包含在发布中,“内容”项目会;例如,在“发布”选项卡上的“应用程序文件”对话框中。

回答by hfrmobile

I am not 100% sure (I read the MSDN description of Build Actionproperty) but just copying that answer from MSDN to StackOverflowdoes not answer the question completely for me.

我不是 100% 确定(我阅读了 MSDN 对Build Action属性的描述)但只是将该答案从 MSDN 复制到StackOverflow并不能完全回答我的问题。

The difference of Noneand Contentonly has an effect on Web projects. For a command line project, WinForm project or UnitTest project (in my case) etc. Noneand Contenthave no different behavior.

NoneContent的区别只对 Web 项目有影响。对于命令行项目、WinForm 项目或 UnitTest 项目(在我的情况下)等。NoneContent没有不同的行为。

MSDN: "project output group" or "Content output group" only terms used in a Web project, right?

MSDN:“项目输出组”或“内容输出组”只是在Web 项目中使用的术语,对吗?

回答by cedd

I have a project that contains no compilable items (it stores html and javascript for jasmine unit tests).

我有一个不包含可编译项目的项目(它存储用于 jasmine 单元测试的 html 和 javascript)。

One day my solution (that contained said project) stopped compiling saying "The target "Build" does not exist in the project".

有一天,我的解决方案(包含所述项目)停止编译,说“项目中不存在目标“Build””。

I added an import to bring in the compiler, which worked fine on my machine but failed using msbuild on the build server.

我添加了一个导入来引入编译器,它在我的机器上运行良好,但在构建服务器上使用 msbuild 失败。

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

I then changed a line from

然后我改变了一条线

<None Include="SpecRunner.html" />

to

<Content Include="SpecRunner.html" />

and it worked on the build server as well.

它也适用于构建服务器。

回答by CAD bloke

You need None in a template project file to include files you define in the .vstemplate otherwise they are lost in the creation & translation process. They get left behind in the temp folder it uses to build everything and then deleted shortly after.

您需要在模板项目文件中使用 None 来包含您在 .vstemplate 中定义的文件,否则它们会在创建和翻译过程中丢失。它们被留在用于构建所有内容的临时文件夹中,然后不久被删除。

回答by DavidRR

In my situation, my MSBuild file had an ItemGroupfor image resources that appeared as follows:

在我的情况下,我的 MSBuild 文件有一个ItemGroup图像资源,如下所示:

  <ItemGroup>
    <Content Include="Resources\image001.png" />
    <Content Include="Resources\image002.png" />
    <Content Include="Resources\image003.png" />
    <Content Include="Resources\image004.png" />
    <None Include="Resources\image005.png" />
    <None Include="Resources\image006.png" />
    <None Include="Resources\image007.png" />
  </ItemGroup>

While my project was building fine, this left me wondering why I had a mix of Contentand Noneitem type elements in my ItemGroup. This MSDN article(for Visual Studio 2010) gave me the guidance I was looking for:

虽然我的项目建设精细,这让我不知道为什么我有一个组合Content,并None在我的项目类型元素ItemGroup。这篇MSDN 文章(适用于 Visual Studio 2010)为我提供了我正在寻找的指导:

Note that when the resource editor adds an image, it sets Build Actionto None, because the .resx file references the image file. At build time, the image is pulled into the .resources file created out of the .resx file. The image can then easily be accessed by way of the strongly-typed class auto-generated for the .resx file. Therefore, you should not change this setting to Embedded Resource, because doing this would include the image two times in the assembly.

请注意,当资源编辑器添加图像时,它会将Build Action设置为None,因为 .resx 文件引用了图像文件。在构建时,图像被拉入从 .resx 文件创建的 .resources 文件中。然后可以通过为 .resx 文件自动生成的强类型类轻松访问该图像。因此,您不应将此设置更改为Embedded Resource,因为这样做会在程序集中两次包含图像。

Resolution:With this guidance, using a text editor, I changed the Contentitem type elements to None.

解决方案:根据本指南,我使用文本编辑器将Content项目类型元素更改为None.

Also, for an overview of MSBuild items, see this MSDN article.

此外,有关 MSBuild 项目的概述,请参阅此MSDN 文章

回答by krishna kanth

In my case .Pubxmlis one of those files among Nonelist. It's not meant for solution building or as a static file for web project. But to publish the site to Azure, the configurations are present in this.

就我而言,.PubxmlNone列表中的文件之一。它不适用于解决方案构建或作为 Web 项目的静态文件。但是要将站点发布到 Azure,配置存在于此。

As per Microsoft article these are the major types we see among .csprojfile tags:

根据 Microsoft 文章,这些是我们在.csproj文件标签中看到的主要类型:

None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.

Compile - The file is compiled into the build output. This setting is used for code files.

Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.

Embedded Resource - This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.

无 - 该文件不包含在项目输出组中,也不会在构建过程中编译。一个示例是包含文档的文本文件,例如自述文件。

编译 - 文件被编译到构建输出中。此设置用于代码文件。

内容 - 文件未编译,但包含在内容输出组中。例如,此设置是 .htm 或其他类型的 Web 文件的默认值。

嵌入资源 - 此文件作为 DLL 或可执行文件嵌入在主项目构建输出中。它通常用于资源文件。

回答by carlin.scott

Contentfiles are not included in a build, but are included in a publish.

内容文件不包含在构建中,但包含在发布中。

Nonefiles are not included in a build or publish, unless they are configured that way by you. For instance, a "Copy to Output Directory" setting of "Always" or "Newer", will cause them to be included in both a build and publish.

None文件不包含在构建或发布中,除非您以这种方式配置它们。例如,“始终”或“较新”的“复制到输出目录”设置将导致它们同时包含在构建和发布中。