Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 537
ilias Avatar asked Sep 03 '12 09:09

ilias


People also ask

What is build action content in Visual Studio?

All files in a Visual Studio project have a build action. The build action controls what happens to the file when the project is compiled. This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see Build actions in Visual Studio for Mac.

What does Copy to Output Directory do?

"Copy to Output Directory" is the property of the files within a Visual Studio project, which defines if the file will be copied to the project's built path as it is. Coping the file as it is allows us to use relative path to files within the project.

How do I change the build output directory in Visual Studio?

To place all solution outputs in a common directory Click on one project in the solution. On the Project menu, click Properties. In each project, depending on its type, select either Compile or Build, and set the Output path or Base output path to a folder to use for all projects in the solution.


2 Answers

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.

like image 193
Oded Avatar answered Oct 07 '22 14:10

Oded


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

like image 35
Martin Prikryl Avatar answered Oct 07 '22 12:10

Martin Prikryl