MSBuild Properties

Properties are key/value pairs that can be used to configure builds. Properties are useful for passing values to tasks, evaluating in conditions, and storing values that will be referenced thoughout the project file.

Defining and Referencing Properties in a Project File

Properties are declared by creating an element with the name of the property as a child of a PropertyGroup element. For example, the following XML creates a property named BuildDir with a value of Build.

<PropertyGroup>
    <BuildDir>Build</BuildDir>
</PropertyGroup>

You reference properties throughout the project file with the syntax $(PropertyName). For example, you reference the property in the previous example with $(BuildDir).

Setting Properties from the Command Line

MSBuild allows you to set properties from the command line using the /property or /p command line switch. Property values received from the command line override property values set in the project file and property values inherited from environment variables.

The following example sets the Configuration property to DEBUG.

MSBuild.exe MyProj.proj /p:Configuration=DEBUG

Reserved Properties

MSBuild reserves some property names to store information about the project file and the MSBuild binaries. These properties are referenced with the $ notation like any other property. For more information, see How to: Reference the Name or Location of the Project File and MSBuild Reserved Properties.

Environment Variables

You can reference environment variables in project files the same way as reserved properties. For example, to use the PATH environment variable in your project file, use $(Path). If the project contains a property definition that has the same name as an environment variable, the property in the project overrides the value of the environment variable. For more information, see How to: Use Environment Variables in a Build.

Registry Properties

You can read system registry values by using the following syntax, where Hive is the registry hive (for example, HKEY_LOCAL_MACHINE ), Key is the key name, SubKey is the subkey name, and Value is the value of the subkey.

$(registry:Hive\MyKey\MySubKey@Value)

To get the default subkey value, omit the Value.

$(registry:Hive\MyKey\MySubKey)

This registry value can be used to initialize a build property. For example, to create a build property that represents the Visual Studio web browser home page, use this code:

<PropertyGroup>

<VisualStudioWebBrowserHomePage>

$(registry:HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\WebBrowser@HomePage)

</VisualStudioWebBrowserHomePage>

<PropertyGroup>

Storing XML in Properties

Properties can contain arbitrary XML, which can aid in passing values to tasks or displaying logging information. The following example shows the ConfigTemplate property with a value containing XML and other property references. MSBuild replaces the property references with their respective property values. Property values are interpreted from top to bottom, so in this example, $(MySupportedVersion), $(MyRequiredVersion), and $(MySafeMode) should have already been defined.

<PropertyGroup>
    <ConfigTemplate>
        <Configuration>
            <Startup>
                <SupportedRuntime
                    ImageVersion="$(MySupportedVersion)" 
                    Version="$(MySupportedVersion)"/>
                <RequiredRuntime
                    ImageVersion="$(MyRequiredVersion)
                    Version="$(MyRequiredVersion)" 
                    SafeMode="$(MySafeMode)"/>
            </Startup>
        </Configuration>
    </ConfigTemplate>
</PropertyGroup>

See Also

Tasks

How to: Use Environment Variables in a Build

How to: Reference the Name or Location of the Project File

Concepts

MSBuild

MSBuild Overview

Reference

MSBuild Reserved Properties

Property Element (MSBuild)