checkwhetheranenvironmentvariablehasbeenset

Cancel Edit [WikiEntry.PreviewButtonText] Save

Check Whether an Environment Variable has been Set

When you build projects, it is often necessary to set build options using information that is not in the project file or the files that comprise your project. This information is typically stored in environment variables.
In this example, environment variables are used in a project file to specify the platform to build for, the location of the source code files, and the location of the output.

Environment Variables

All environment variables are available to the MSBuild project file as properties. Consider, for example, a development team where all members want to use the same project file to build their code. However, each team member can place his or her source files at a different location on disk. To avoid having to customize the project file for each team member, instead each team member sets an environment variable on his or her computer that specifies the location of the source code. The project file can then reference that environment variable.
Also, because the location of the source files is critical information without which the build cannot proceed, the project file can enforce the presence of that information by using the Error, Warning, and Message elements tags in conjunction with the Condition attribute.

Error, Warning, and Message Elements

Using the Error element, you can check for the presence of a required configuration or property before a target is run. If the Condition defined in the Error element is true, an error event is raised with the specified message, and the build stops. In this example, an error is used to report to the user that the source code location was not specified on the command line:

<Error
Condition="'$(SourceCodeLocation)' == ''"
Text="The location of the source code was not specified."/>

Using the Warning element, you can check for the presence of a required configuration or property before a target is run. If the Condition defined in the Warning element is true, a warning event is raised with the specified message, but the build continues. In this example, a warning is used to report to the user that the default value has been set for the Platform property because the user did not specify a value on the command line or in the environment:

<Warning
Condition="'$(Platform)' == ''"
Text="Platform not specified, defaulting to x86"/>

Some status information is automatically logged by MSBuild as a build progresses, for example, the current target and task. In addition, you can use the Message element to define other information that you want to log. In this example, the values that the user has specified for three properties is reported:

<Message Text="The code at $(SourceCodeLocation) will be built to $(OutputDir) for platform $(Platform)"/>

The Error, Warning, and Message elements all have two attributes: Text and Condition. The required Text attribute specifies the message that is displayed if, and only if, the condition is met. The Condition attribute is optional.

Condition Attribute

The Condition attribute can be used with many elements in a project file. A condition can be used to enable or disable one property, for example, if the value of the Flavor property is DEBUG, the DebugType property is defined:

<DebugType Condition="'$(Flavor)'=='DEBUG'">full</DebugType>

When a condition is applied at the PropertyGroup level, a Condition can be used to enable or disable all the properties contained within it, for example:

<PropertyGroup Condition="'$(Flavor)'=='DEBUG'">
<DebugType>full</DebugType>
<Optimize>off</Optimize>
</PropertyGroup>

Property groups are especially useful when you want to apply the same condition to a large number of properties in a project file.

Four kinds of conditions can be used in a project file:
* equal
* not equal
* exists
* not exists

Condition Syntax

For all conditions, the evaluated text is enclosed in single quotes.
Evaluation of conditions is not case sensitive. The following conditions are equivalent in an MSBuild project file.

Condition="'$(Flavor)'=='DEBUG'"
and
Condition="'$(flavor)'=='debug'"

Equal Condition


Syntax:
Condition="'<some text>'=='<some text>'"

Example:
<DebugType Condition="'$(Flavor)'=='DEBUG'">full</DebugType>

Not Equal Condition


Syntax:
Condition="'<some text>'!='<some text>'"
Example:
<Optimize Condition="'$(Flavor)'!='DEBUG'">on</Optimize>

Exists Condition

Instead of evaluating text, the exists and not exists conditions use the file system to determine whether a file exists. The <text> must refer to an actual file location and name; otherwise the condition equals false.

Syntax:
Condition="Exists('<text>')"

Example:
<MyAppHasBuilt Condition="Exists('MyApp.exe')">yes</MyAppHasBuilt>

Not Exists Condition


Syntax:
Condition="!Exists('<text>')"
Example:
<MyAppHasBuilt Condition="!Exists('MyApp.exe')">no</MyAppHasBuilt>

Project Files

Download Code

Example

<Project DefaultTargets="FakeBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- Ideally the platform should be set in order to continue -->
<Warning Condition="'$(Platform)' == ''" Text="Platform not specified, defaulting to x86"/>

<PropertyGroup>
<Platform Condition="'$(Platform)' == ''">x86</Platform>
</PropertyGroup>

<!-- The location of the source code MUST be specified -->
<Error Condition="'$(SourceCodeLocation)' == ''" Text="The location of the source code was not specified."/>

<!-- The location of the output MUST be specified -->
<Error Condition="'$(OutputDir)' == ''" Text="The location of the output was not specified."/>

<Target Name="FakeBuild">
<Message Text="The code at $(SourceCodeLocation) will be built to $(OutputDir) for platform $(Platform)"/>
</Target>

</Project>
Microsoft Communities