What are the various "Build action" settings in Visual Studio project properties and what do they do?
Asked Answered
K

8

910

For the most part, you just take whatever Visual Studio sets it for you as a default... I'm referring to the BuildAction property for each file selected in Solution Explorer. There are a number of options and it's difficult to know what each one of them will do.

Kalinda answered 28/9, 2008 at 12:15 Comment(0)
K
1111
  • 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: Allows you to retrieve a file (in the same directory as the assembly) as a stream via Application.GetContentStream(URI). For this method to work, it needs a AssemblyAssociatedContentFile custom attribute which Visual Studio graciously adds when you mark a file as "Content"

  • Embedded resource: Embeds the file in an exclusive assembly manifest resource.

  • Resource (WPF only): Embeds the file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName.g.resources.

  • Page (WPF only): Used to compile a xaml file into baml. The baml is then embedded with the same technique as Resource (i.e. available as `AppName.g.resources)

  • ApplicationDefinition (WPF only): Mark the XAML/class file that defines your application. You specify the code-behind with the x:Class="Namespace.ClassName" and set the startup form/page with StartupUri="Window1.xaml"

  • SplashScreen (WPF only): An image that is marked as SplashScreen is shown automatically when an WPF application loads, and then fades

  • DesignData: Compiles XAML viewmodels so that usercontrols can be previewed with sample data in Visual Studio (uses mock types)

  • DesignDataWithDesignTimeCreatableTypes: Compiles XAML viewmodels so that usercontrols can be previewed with sample data in Visual Studio (uses actual types)

  • EntityDeploy: (Entity Framework): used to deploy the Entity Framework artifacts

  • CodeAnalysisDictionary: An XML file containing custom word dictionary for spelling rules

Kalinda answered 28/9, 2008 at 12:29 Comment(10)
@Gishu: Page is for a certain type of XAML file, but most XAML files fall under it anyways.Sang
"DesignData" is used for "Blendability", allowing you to add WPF mark-up based Data, which is, when the correct settings are applied, then shown at Design Time.Oversize
Also note that Content will be included when using one-click deploy, but None won't even if "copy if newer" is selected.Capsule
What about Fakes, CodeAnalysisDictionary and XamlAppRef?Amentia
For details on Content action see #22553089Marrymars
AssemblyAssociatedContentFile is only for WPF projects, so it is still a bit unclear what Content Action and Content output group is all about ...Homoiousian
What about AdditionalFiles? I see it in VS 2015 but couldn't find a reference for that.Astrolabe
It's also useful to point out that you can customize your own build action: blogs.msdn.microsoft.com/msbuild/2005/10/06/…Ullyot
AdditionalFiles are described in #40030129Amboceptor
what to use for log4net config and other XML config files?Aires
J
119

From the documentation:

The BuildAction property indicates what Visual Studio does with a file when a build is executed. BuildAction can have one of several values:

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.

Justification answered 28/9, 2008 at 12:20 Comment(6)
Interesting! I have VS2008 installed, I wonder why they didn't appear in my documentation?Justification
What about PRI resource?Enterpriser
@PaulBatum can you insert the reference for this quoted definition ?Console
@Raymond 232: What quoted definition?Eliathan
@PaulBatum: Yes indeed the reference would be nice. The answer starts with "From the documentation" so a link to this documentation would be appreciated.Zibet
My friends, this answer was from 2008. Back then the documentation wasn't online - it was installed on my machine! Give me a break :)Justification
O
36

Page -- Takes the specified XAML file, and compiles into BAML, and embeds that output into the managed resource stream for your assembly (specifically AssemblyName.g.resources), Additionally, if you have the appropriate attributes on the root XAML element in the file, it will create a blah.g.cs file, which will contain a partial class of the "codebehind" for that page; this basically involves a call to the BAML goop to re-hydrate the file into memory, and to set any of the member variables of your class to the now-created items (e.g. if you put x:Name="foo" on an item, you'll be able to do this.foo.Background = Purple; or similar.

ApplicationDefinition -- similar to Page, except it goes onestep furthur, and defines the entry point for your application that will instantiate your app object, call run on it, which will then instantiate the type set by the StartupUri property, and will give your mainwindow.

Also, to be clear, this question overall is infinate in it's results set; anyone can define additional BuildActions just by building an MSBuild Task. If you look in the %systemroot%\Microsoft.net\framework\v{version}\ directory, and look at the Microsoft.Common.targets file, you should be able to decipher many more (example, with VS Pro and above, there is a "Shadow" action that allows you generate private accessors to help with unit testing private classes.

Off answered 28/9, 2008 at 16:12 Comment(1)
i hope you don't mind that i copied parts of your answer into Gishu's answer, in order to get a more complete reference answer.Domella
D
35

VS2010 has a property for 'Build Action', and also for 'Copy to Output Directory'. So an action of 'None' will still copy over to the build directory if the copy property is set to 'Copy if Newer' or 'Copy Always'.

So a Build Action of 'Content' should be reserved to indicate content you will access via 'Application.GetContentStream'

I used the 'Build Action' setting of 'None' and the 'Copy to Output Direcotry' setting of 'Copy if Newer' for some externally linked .config includes.

G.

Devora answered 9/4, 2013 at 21:37 Comment(3)
Thanks for this. However I am a little confused as to how/where I do this. Ideally I would like to just "include" my "Upload" folder, and all child files will be included in the publish.Philpott
This applies to the properties of an individual item in the object explorer, which you'll see in the properties tab when you select an item or right click and select properties. For what you want, I think you just have to add existing item when you right click the project. BTW, the add existing item dialog's save button has a drop down that allows you to add it as a link. You still may have to add the items within the folder.Devora
I'm glad I got an upvote recently, but there must have been a consolidation of questions and answers. My answer doesn't make sense with this more general question - I was specifically answering a question about buildaction vs copy.Devora
R
5

In VS2008, the doc entry that seems the most useful is:

Windows Presentation Foundation Building a WPF Application (WPF)

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/wpf_conceptual/html/a58696fd-bdad-4b55-9759-136dfdf8b91c.htm

ApplicationDefinition Identifies the XAML markup file that contains the application definition (a XAML markup file whose root element is Application). ApplicationDefinition is mandatory when Install is true and OutputType is winexe. A WPF application and, consequently, an MSBuild project can only have one ApplicationDefinition.

Page Identifies a XAML markup file whose content is converted to a binary format and compiled into an assembly. Page items are typically implemented in conjunction with a code-behind class.

The most common Page items are XAML files whose top-level elements are one of the following:

Window (System.Windows..::.Window).

Page (System.Windows.Controls..::.Page).

PageFunction (System.Windows.Navigation..::.PageFunction<(Of <(T>)>)).

ResourceDictionary (System.Windows..::.ResourceDictionary).

FlowDocument (System.Windows.Documents..::.FlowDocument).

UserControl (System.Windows.Controls..::.UserControl).

Resource Identifies a resource file that is compiled into an application assembly. As mentioned earlier, UICulture processes Resource items.

Content Identifies a content file that is distributed with an application. Metadata that describes the content file is compiled into the application (using AssemblyAssociatedContentFileAttribute).

Rahal answered 31/3, 2009 at 17:53 Comment(0)
L
5

Build actions control the MSBuild Item Type of each item in a project. For example, a Compile build action on MyClass.cs means something like this in your .csproj file:

<ItemGroup>
  <Compile>MyClass.cs</Compile>
</ItemGroup>

Item types have specific meanings by convention. Common types are Compile, Content and None, but there are others.

For example, .editorconfig files have their own item type (EditorConfigFiles). Files may be passed to analyzers by marking them with "C# analyzer additional file" (AdditionalFiles).

You can also define your own item types in your project for your own purposes via AvailableItemName. For example:

<ItemGroup>
  <AvailableItemName Include="Foo" />
</ItemGroup>

Doing this will produce:

enter image description here

Lexielexigraphy answered 13/5, 2021 at 1:34 Comment(0)
P
4

How about this page from Microsoft Connect (explaining the DesignData and DesignDataWithDesignTimeCreatableTypes) types. Quoting:

The following describes the two Build Actions for Sample Data files.

Sample data .xaml files must be assigned one of the below Build Actions:

DesignData: Sample data types will be created as faux types. Use this Build Action when the sample data types are not creatable or have read-only properties that you want to defined sample data values for.

DesignDataWithDesignTimeCreatableTypes: Sample data types will be created using the types defined in the sample data file. Use this Build Action when the sample data types are creatable using their default empty constructor.

Not so incredibly exhaustive, but it at least gives a hint. This MSDN walkthrough also gives some ideas. I don't know whether these Build Actions are applicable for non-Silverlight projects also.

Panay answered 10/10, 2013 at 7:1 Comment(1)
First link in answer is outdated - "Microsoft Connect Has Been Retired".Leicestershire
L
4
  • Fakes: Part of the Microsoft Fakes (Unit Test Isolation) Framework. Not available on all Visual Studio versions. Fakes are used to support unit testing in your project, helping you isolate the code you are testing by replacing other parts of the application with stubs or shims. More here: https://msdn.microsoft.com/en-us/library/hh549175.aspx
Letitialetizia answered 26/1, 2016 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.