在 csproj 文件中,我们可以使用 None
或 Content
元素包含一个文件。来自 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.
但由于 None
或 Content
元素可以包含一个 CopyToOutputDirectory
元素,因此我想知道我是否将其设置为 Always
,None
和 Content
的行为是否相同?
最佳答案
通过设置 CopyToOutputDirectory
,并非复制到输出目录的所有内容都会复制到Content Output Group。因此,您可以这样做:
File1---CopyToOutputDirectory = Copy always, Content
File2---CopyToOutputDirectory = Copy always, Content
File3---CopyToOutputDirectory = Copy always, None
所有三个文件都将被复制到输出目录,但只有 File1
和 File2
将被复制到 Content Output Group。
此外,Content
允许您通过 Application.GetContentStream(URI)
以流的形式检索文件(与程序集位于同一目录中)。要使此方法起作用,它需要一个 AssemblyAssociatedContentFile
自定义属性,当您将文件标记为 Content
时,Visual Studio 会优雅地添加该属性。
None
和 Content
是文件如何与构建和部署过程相关的值。因此,您的构建(例如 MS Build ) 和部署可能与简单地从输出目录中获取文件有很大不同。您可能在输出目录中有一个您并不真正需要但部署时需要的 .bat 文件。
This SO answer 包含有关不同构建操作的更多详细信息。
关于c# - 设置 "Always copy to output directory"时,Content 和 None 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41754602/