Difference between Build action content and 'Copy to output directory' in Visual Studio

会有一股神秘感。 提交于 2019-11-29 04:38:34

问题


In my project in Visual Studio, I have files that I want included in the output, but not compiled or embedded.

I am aware that there are 2 ways to accomplish this.

  1. Setting the Build Action for the file to 'Content'
  2. Setting the 'Copy to Output Directory' to 'Copy Always' or 'Copy if newer'

Can someone tell me what the difference is and which scenario is each of the options appropriate for?


回答1:


When setting the Build Action to Content, the file will not be compiled and will be put in the Content output group.

Whether the file gets copies or not depends on the Copy to Output Directory setting at that point.

Of course, if you set Copy Always on a file, it may get compiled and then copied.

See File Properties on MSDN:

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.




回答2:


The Content build action has visible effect in WPF projects (possibly ASP too).

It adds

[assembly: System.Windows.Resources.AssemblyAssociatedContentFileAttribute("filename")]

to WpfApplication1_Content.g.cs. Read about AssemblyAssociatedContentFileAttribute.

In WinForms and console application (what OP may be using) it does not do this, so there's no difference to None action when building.

In this comment, I've found also a note about effect on deployment:

Also note that Content will be included when using one-click deploy, but None won't even if "copy if newer" is selected.

Possibly this works even for console and WinForms applications (I haven't tried).




回答3:


Copy Always will also trigger a rebuild of that project in a solution even if no code has changed.



来源:https://stackoverflow.com/questions/12245208/difference-between-build-action-content-and-copy-to-output-directory-in-visual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!