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

StackOverflow https://stackoverflow.com/questions/12245208

  •  29-06-2021
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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).

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top